Skip to content

Commit

Permalink
fix: validation
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed Nov 3, 2020
1 parent c81c378 commit 354458b
Show file tree
Hide file tree
Showing 6 changed files with 466 additions and 378 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
"@commitlint/config-conventional": "^11.0.0",
"@types/jest": "^26.0.15",
"@types/node": "^14.14.6",
"@typescript-eslint/eslint-plugin": "^4.6.0",
"@typescript-eslint/parser": "^4.6.0",
"@typescript-eslint/eslint-plugin": "^4.6.1",
"@typescript-eslint/parser": "^4.6.1",
"cross-env": "^7.0.2",
"eslint": "^7.12.1",
"husky": "^4.3.0",
"jest": "^26.6.1",
"jest": "^26.6.2",
"lerna": "^3.22.1",
"ts-jest": "^26.4.3",
"typescript": "^4.0.5"
Expand Down
19 changes: 19 additions & 0 deletions packages/unit/src/Validation/ValidationSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class ValidationSchema<T> {
protected readonly attributes: ValidationAttributes = {
required: true,
nullable: false,
clear: true,
};

protected readonly rules = new Map<keyof T, ValidationRule<T, any>>();
Expand All @@ -20,6 +21,15 @@ export class ValidationSchema<T> {
}
}

public getAttribute<K extends keyof ValidationAttributes>(key: K): ValidationAttributes[K] {
return this.attributes[key];
}

public setAttribute<K extends keyof ValidationAttributes>(key: K, value: ValidationAttributes[K]): this {
this.attributes[key] = value;
return this;
}

public add<K extends keyof T>(key: K, options: ValidatorArg<T, K>): ValidationSchema<T> {
const rule = this.ensure(key);
if (isFunction(options) || options instanceof ValidationSchema) {
Expand Down Expand Up @@ -55,10 +65,19 @@ export class ValidationSchema<T> {
validations.push([key, await rule.validate(state[key])]);
}

if (this.attributes.clear && isObject(state)) {
for (const key of Object.keys(state)) {
if (!this.rules.has(key as keyof T)) {
delete state[key];
}
}
}

return {
message,
valid: !validations.some(([, value]) => !value.valid),
validation: entriesReverse<ValidationResult<T, keyof T>>(validations),
state,
};
} finally {
finish();
Expand Down
2 changes: 2 additions & 0 deletions packages/unit/src/Validation/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type ValidationFunction<T, K extends keyof T> = |
export type ValidationAttributes = {
required?: boolean;
nullable?: boolean;
clear?: boolean;
};

export type ValidatorArg<T, K extends keyof T> = ValidationFunction<T, K>
Expand All @@ -32,6 +33,7 @@ export type ValidationDescription<T> = {
valid: boolean;
message?: string;
validation: { [K in keyof T]: ValidationResult<T, K> };
state: T;
};

export type ValidationResult<T, K extends keyof T> = { valid: true } |
Expand Down
9 changes: 7 additions & 2 deletions packages/unit/test/src/Validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface ISample {
nullable: string | null;
}

const validSample: ISample = {
const validSample: ISample & Record<string, any> = {
v: 1,
b: true,
str: "foo",
Expand All @@ -30,6 +30,7 @@ const validSample: ISample = {
arrayReq: [1, 2, 3],
array: ["foo"],
nullable: null,
alien: "should be removed",
};

const invalidSample = {
Expand All @@ -42,6 +43,7 @@ const invalidSample = {
arrayReq: [],
array: [],
nullable: true,
alien: "should not be removed",
};

class IChildSampleValidation extends ValidationSchema<IChildSample> {
Expand Down Expand Up @@ -76,7 +78,10 @@ test("Validation", async () => {
expect(success).toMatchSnapshot();
expect(success.valid).toBe(true);

const fails = await validationSchema.validate(invalidSample);
const fails = await validationSchema
.setAttribute("clear", false)
.validate(invalidSample);

expect(fails).toMatchSnapshot();
expect(fails.valid).toBe(false);

Expand Down
48 changes: 48 additions & 0 deletions packages/unit/test/src/__snapshots__/Validation.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,29 @@
exports[`Validation 1`] = `
Object {
"message": undefined,
"state": Object {
"array": Array [
"foo",
],
"arrayReq": Array [
1,
2,
3,
],
"b": true,
"child": Object {
"date": "2020-01-01T00:00:00.000Z",
"name": "Bob",
},
"emptyStrArr": Array [],
"nullable": null,
"str": "foo",
"v": 1,
},
"valid": true,
"validation": Object {
"array": Object {
"clear": true,
"nullable": true,
"required": true,
"valid": true,
Expand All @@ -14,6 +34,7 @@ Object {
],
},
"arrayReq": Object {
"clear": true,
"nullable": false,
"required": true,
"valid": true,
Expand All @@ -35,18 +56,21 @@ Object {
},
},
"child2": Object {
"clear": true,
"nullable": false,
"required": false,
"valid": true,
"value": undefined,
},
"emptyStrArr": Object {
"clear": true,
"nullable": true,
"required": false,
"valid": true,
"value": Array [],
},
"nullable": Object {
"clear": true,
"nullable": true,
"required": true,
"valid": true,
Expand All @@ -67,15 +91,29 @@ Object {
exports[`Validation 2`] = `
Object {
"message": undefined,
"state": Object {
"alien": "should not be removed",
"array": Array [],
"arrayReq": Array [],
"b": true,
"child": Object {
"date": "2020-01-01T00:00:00.000Z",
"name": 123,
},
"nullable": true,
"v": null,
},
"valid": false,
"validation": Object {
"array": Object {
"clear": true,
"nullable": true,
"required": true,
"valid": true,
"value": Array [],
},
"arrayReq": Object {
"clear": true,
"nullable": false,
"required": true,
"valid": false,
Expand All @@ -86,19 +124,25 @@ Object {
"value": true,
},
"child": Object {
"clear": true,
"message": undefined,
"nullable": false,
"required": true,
"valid": false,
"validation": Object {
"message": undefined,
"state": Object {
"date": "2020-01-01T00:00:00.000Z",
"name": 123,
},
"valid": false,
"validation": Object {
"date": Object {
"valid": true,
"value": "2020-01-01T00:00:00.000Z",
},
"name": Object {
"clear": true,
"error": [Error: Assertion fails],
"message": undefined,
"nullable": false,
Expand All @@ -114,12 +158,14 @@ Object {
},
},
"child2": Object {
"clear": true,
"nullable": false,
"required": false,
"valid": true,
"value": undefined,
},
"emptyStrArr": Object {
"clear": true,
"nullable": true,
"required": false,
"valid": true,
Expand All @@ -130,12 +176,14 @@ Object {
"value": true,
},
"str": Object {
"clear": true,
"nullable": false,
"required": false,
"valid": true,
"value": undefined,
},
"v": Object {
"clear": true,
"error": [Error: Assertion fails],
"message": "The value of \\"v\\" shouldn't be null",
"nullable": false,
Expand Down
Loading

0 comments on commit 354458b

Please sign in to comment.