Skip to content

Commit

Permalink
feat(validator): original for validate
Browse files Browse the repository at this point in the history
  • Loading branch information
deot committed Sep 20, 2024
1 parent 69c43ad commit f25a58d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
29 changes: 29 additions & 0 deletions packages/validator/__tests__/original.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Validator } from '@deot/helper-validator';

describe('validator.ts', () => {
it('original', async () => {
expect.hasAssertions();
const message1 = 'age > 16 & age < 30';
const validator = new Validator(
{
age: {
message: message1,
validate: (value, { original }) => {
return value < 30 && value > original.min;
}
},
},
[],
{
min: 16
}
);

try {
await validator.validate({ age: 10 });
} catch (e: any) {
expect(e.length).toBe(1);
expect(e[0].message).toBe(message1);
}
});
});
16 changes: 12 additions & 4 deletions packages/validator/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export interface ValidateContext {

// 用于 object / array 在当前source的寻址
paths: Path[];

original: any;
}
export type Transform = (value: any, context: ValidateContext) => any;
export type Validate = (value: any, context: ValidateContext) => Promise<any> | boolean | string | void;
Expand All @@ -31,6 +33,8 @@ export interface ValidateOptions {

// 内部使用,用于数组是记录path
_index?: number;

original?: any;
}

export interface ValidateError {
Expand Down Expand Up @@ -61,9 +65,12 @@ export class Validator {

paths: Path[];

constructor(rules?: ValidatorRules, paths?: Path[]) {
original: any;

constructor(rules?: ValidatorRules, paths?: Path[], original?: any) {
this.updateRules(rules);
this.paths = paths || [];
this.original = original;
}

/**
Expand Down Expand Up @@ -142,6 +149,7 @@ export class Validator {
const field = needCheckFields[i];
const rules = this.rules[field];
const value = source[field];
const original = this.original;

for (let j = 0; j < rules.length; j++) {
const rule = rules[j];
Expand All @@ -152,7 +160,7 @@ export class Validator {
paths.splice(paths.length - 1, 0, options._index);
}

const value$ = transform!(value, { field, source, paths });
const value$ = transform!(value, { field, source, paths, original });

if (fields$) {
const isArray = Array.isArray(value$);
Expand All @@ -161,7 +169,7 @@ export class Validator {
const validator = new Validator(fields$, paths);
for (let k = 0; k < value$$.length; k++) {
try {
await validator.validate(value$$[k], { _index: isArray ? k : undefined, first: options.first });
await validator.validate(value$$[k], { _index: isArray ? k : undefined, first: options.first, original });
} catch (errors$: any) {
errors.push(...errors$);
if (errors$.length && options.first) break;
Expand All @@ -171,7 +179,7 @@ export class Validator {
}
}

let result = validate!(value$, { field, source, paths });
let result = validate!(value$, { field, source, paths, original });
let message$ = '';
if (result instanceof Promise) {
try {
Expand Down

0 comments on commit f25a58d

Please sign in to comment.