Skip to content

Commit

Permalink
fix: array validation
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed Mar 8, 2020
1 parent 282715f commit 8ba0dda
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
9 changes: 3 additions & 6 deletions packages/unit/src/Validation/ValidationRule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {assert, fails, isFunction, isNull, isUndefined} from "@typesafeunit/util";
import {ValidationAttributes, ValidationFunction, ValidationResult} from "./interfaces";
import {ValidationAttributes, ValidationFails, ValidationFunction, ValidationResult} from "./interfaces";
import {ValidationSchema} from "./ValidationSchema";

export class ValidationRule<T, K extends keyof T> {
Expand Down Expand Up @@ -61,10 +61,7 @@ export class ValidationRule<T, K extends keyof T> {
for (const [validator, message] of [...validators, ...this.validators]) {
try {
if (isFunction(validator)) {
const returns = validator(value);
if (!isUndefined(returns)) {
returns;
}
validator(value);
continue;
}

Expand All @@ -75,7 +72,7 @@ export class ValidationRule<T, K extends keyof T> {

const validation = await validator.validate(value);
if (!validation.valid) {
return {value, message, validation, valid: false, ...attributes};
return {value, message, validation, valid: false, ...attributes} as ValidationFails<any, any>;
}
} catch (error) {
return {value, error, message, valid: false, ...attributes};
Expand Down
6 changes: 1 addition & 5 deletions packages/unit/src/Validation/ValidationSchema.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import {entriesReverse, isFunction, isObject, isUndefined} from "@typesafeunit/util";
import {ValidationAttributes, ValidationDescription, ValidationFunction, ValidationResult} from "./interfaces";
import {ValidationAttributes, ValidationDescription, ValidationResult, ValidatorArg} from "./interfaces";
import {ValidationError} from "./ValidationError";
import {ValidationRule} from "./ValidationRule";

type ValidatorArg<T, K extends keyof T> = ValidationFunction<T, K>
| [ValidationFunction<T, K>, string]
| ValidationAttributes & { validator: ValidationFunction<T, K>; message?: string };

export class ValidationSchema<T> {
protected readonly attributes: ValidationAttributes = {
required: true,
Expand Down
14 changes: 11 additions & 3 deletions packages/unit/src/Validation/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import {ValidationSchema} from "./ValidationSchema";

export type ValidationFunction<T, K extends keyof T> = ((value: T[K]) => void) | ValidationSchema<T[K]>;
export type ValidationValue<T> = T extends Array<infer V> ? V : T;

export type ValidationFunction<T, K extends keyof T> = |
((value: T[K]) => void) | ValidationSchema<ValidationValue<T[K]>>;

export type ValidationAttributes = {
required?: boolean;
nullable?: boolean;
};

export type ValidatorArg<T, K extends keyof T> = ValidationFunction<T, K>
| [ValidationFunction<T, K>, string]
| ValidationAttributes & { validator: ValidationFunction<T, K>; message?: string };


export type ValidationSuccess<T, K extends keyof T> = {
value: T[K];
valid: true;
};

export type ValidationFails<T, K extends keyof T> = {
value?: unknown;
value?: any;
valid: false;
message?: string;
error?: Error;
Expand All @@ -23,7 +32,6 @@ export type ValidationResult<T, K extends keyof T> = { valid: true } |
ValidationSuccess<T, K> |
ValidationFails<T, K>;


export type ValidationDescription<T> = {
valid: boolean;
message?: string;
Expand Down

0 comments on commit 8ba0dda

Please sign in to comment.