Skip to content

Commit

Permalink
Return back the validated data
Browse files Browse the repository at this point in the history
  • Loading branch information
ooade committed Mar 7, 2020
1 parent 899088a commit 5a38518
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
49 changes: 35 additions & 14 deletions __test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from "tape";
import validator, { CustomErrorHandlers, Schema } from "../src";
import yooda, { CustomErrorHandlers, Schema } from "../src";

const domains = {
IS_INTEGER: (value: number) => Number.isInteger(value),
Expand Down Expand Up @@ -45,14 +45,14 @@ const schemas: Schemas = {
};

test("should not throw error with the right data types and schema", t => {
const validate = validator(schemas.ageRequiredAndHeight);
const validate = yooda(schemas.ageRequiredAndHeight);
t.doesNotThrow(() => validate({ age: 2, height: 3 }));
t.end();
});

test("should throw error when a required data isn't supplied", t => {
try {
const validate = validator(schemas.ageRequiredAndHeight);
const validate = yooda(schemas.ageRequiredAndHeight);
validate({ height: 3 });
} catch (err) {
t.equal(err.message, "age is required", "returns the right message");
Expand All @@ -62,7 +62,7 @@ test("should throw error when a required data isn't supplied", t => {

test("should throw error when a required data type isn't supplied", t => {
try {
const validate = validator(schemas.ageRequired);
const validate = yooda(schemas.ageRequired);
validate({ age: "3" });
} catch (err) {
t.equal(
Expand All @@ -76,7 +76,7 @@ test("should throw error when a required data type isn't supplied", t => {

test("should throw error when a value does not meet one of the domain's requirement", t => {
try {
const validate = validator(schemas.agePositiveDomainRequiredAndHeight);
const validate = yooda(schemas.agePositiveDomainRequiredAndHeight);
validate({ age: -3 });
} catch (err) {
t.equal(
Expand All @@ -94,7 +94,7 @@ test("should use custom required error message", t => {
};

try {
const validate = validator(
const validate = yooda(
schemas.agePositiveDomainRequiredAndHeight,
CustomErrorHandlers
);
Expand All @@ -116,10 +116,7 @@ test("should use custom type error message", t => {
};

try {
const validate = validator(
schemas.ageRequiredAndHeight,
CustomErrorHandlers
);
const validate = yooda(schemas.ageRequiredAndHeight, CustomErrorHandlers);
validate({ age: "12", height: 2 });
} catch (err) {
t.equal(
Expand All @@ -138,7 +135,7 @@ test("should use custom domain error message", t => {
};

try {
const validate = validator(
const validate = yooda(
schemas.agePositiveDomainRequiredAndHeight,
CustomErrorHandlers
);
Expand All @@ -164,7 +161,7 @@ test("should throw an error if the domain is not an array", t => {
};

try {
const validate = validator(schema);
const validate = yooda(schema);
validate({ age: 10 });
} catch (err) {
t.equal(
Expand All @@ -187,14 +184,38 @@ test("should throw an error if one of the domains isn't a function", t => {
};

try {
const validate = validator(schema);
const validate = yooda(schema);
validate({ age: 2 });
} catch (err) {
t.equal(
err.message,
"domain is not a function",
"currentDomain is not a function",
"returns the right message"
);
}
t.end();
});

test("should throw an error if no schema is passed", t => {
try {
// @ts-ignore
const validate = yooda();
validate({ age: 2 });
} catch (err) {
t.equal(
err.message,
"Schema object not found!",
"returns the right message"
);
}
t.end();
});

test("should return back the validated data", t => {
const validate = yooda(schemas.ageRequiredAndHeight);
const validated = validate({ age: 3, height: 4.5 });

t.equal(validated.age, 3);
t.equal(validated.height, 4.5);
t.end();
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yooda",
"version": "0.3.0",
"version": "0.4.0",
"description": "🧘‍♂️600b validation library with custom handlers and messages",
"source": "src/index.ts",
"main": "build/index.js",
Expand Down
29 changes: 15 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,7 @@ const validator = (
) => {
const handler: ProxyHandler<Data> = {
set(target, key: string, value: any) {
const targetSchema = schema[key];

// Can't trap without a schema
if (!targetSchema) return true;

const type = targetSchema.type;
const domainArr = targetSchema.domain;
const { type, domain } = schema[key];

if (typeOf(value) !== type) {
throw new TypeError(
Expand All @@ -60,33 +54,38 @@ const validator = (
);
}

if (domainArr && typeOf(domainArr) !== "array") {
if (domain && typeOf(domain) !== "array") {
throw new Error(`The domain set on ${key} is expected to be an array`);
}

if (domainArr) {
domainArr.forEach(domain => {
if (!domain(value)) {
if (domain) {
domain.forEach(currentDomain => {
if (!currentDomain(value)) {
throw new Error(
customErrorHandlers?.domainError
? customErrorHandlers.domainError({
key,
value: JSON.stringify(value),
domain: domain.name
domain: currentDomain.name
})
: `${value} assigned to ${key} does not satisfy the ${domain.name} domain`
: `${value} assigned to ${key} does not satisfy the ${currentDomain.name} domain`
);
}
});
}

target[key] = value;
return true;
}
};

return (data: Data): void => {
return (data: Data) => {
const proxy = new Proxy({}, handler);

if (!(schema && typeOf(schema) === "object")) {
throw new Error("Schema object not found!");
}

Object.keys(schema).forEach(key => {
const required = schema[key].required;

Expand All @@ -102,6 +101,8 @@ const validator = (
Object.keys(data).forEach(key => {
proxy[key] = data[key];
});

return proxy;
};
};

Expand Down

0 comments on commit 5a38518

Please sign in to comment.