Skip to content

Commit

Permalink
test: add tests for rule priority
Browse files Browse the repository at this point in the history
  • Loading branch information
simonguo committed Apr 12, 2024
1 parent 19f168b commit 176e04a
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/MixedType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ export class MixedType<ValueType = any, DataType = any, E = ErrorMessageType, L
this.fieldLabel
);

const checkStatus = validator(value, this.priorityRules);
const checkResult = validator(value, this.priorityRules);

if (checkStatus) {
return checkStatus;
// If the priority rule fails, return the result directly
if (checkResult) {
return checkResult;
}

if (!this.required && isEmpty(value)) {
Expand Down Expand Up @@ -124,9 +125,10 @@ export class MixedType<ValueType = any, DataType = any, E = ErrorMessageType, L

return new Promise(resolve =>
validator(value, this.priorityRules)
.then((checkStatus: CheckResult<E | string> | void | null) => {
if (checkStatus) {
resolve(checkStatus);
.then((checkResult: CheckResult<E | string> | void | null) => {
// If the priority rule fails, return the result directly
if (checkResult) {
resolve(checkResult);
}
})
.then(() => {
Expand All @@ -135,9 +137,9 @@ export class MixedType<ValueType = any, DataType = any, E = ErrorMessageType, L
}
})
.then(() => validator(value, this.rules))
.then((checkStatus: CheckResult<E | string> | void | null) => {
if (checkStatus) {
resolve(checkStatus);
.then((checkResult: CheckResult<E | string> | void | null) => {
if (checkResult) {
resolve(checkResult);
}
resolve({ hasError: false });
})
Expand Down
68 changes: 68 additions & 0 deletions test/MixedTypeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,74 @@ describe('#MixedType', () => {
schema2.checkForField('str', { str: '12' }).hasError.should.equal(true);
schema2.checkForField('str', { str: '12' }).errorMessage.should.equal('error');
});

describe('priority - async', () => {
it('Should have the correct priority', async () => {
const schema = SchemaModel({
name: StringType()
.isEmail('error1')
.addRule(() => false, 'error2')
});

const result = await schema.checkAsync({ name: 'a' });

expect(result).to.deep.equal({
name: { hasError: true, errorMessage: 'error1' }
});

const schema2 = SchemaModel({
name: StringType()
.isEmail('error1')
.addRule(() => false, 'error2', true)
});

const result2 = await schema2.checkAsync({ name: 'a' });

expect(result2).to.deep.equal({
name: { hasError: true, errorMessage: 'error2' }
});

const schema3 = SchemaModel({
name: StringType().addRule(() => true, 'error2', true)
});

const result3 = await schema3.checkAsync({ name: 'a' });

expect(result3).to.deep.equal({
name: { hasError: false }
});
});

it('Should be isRequired with a higher priority than addRule', async () => {
const schema = SchemaModel({
str: StringType()
.isRequired('required')
.addRule(value => value === '', 'error')
});

const result = await schema.checkAsync({ str: '' });

expect(result).to.deep.equal({
str: { hasError: true, errorMessage: 'required' }
});

const result2 = await schema.checkAsync({ str: '12' });

expect(result2).to.deep.equal({
str: { hasError: true, errorMessage: 'error' }
});

const schema2 = SchemaModel({
str: StringType().addRule(value => value === '', 'error')
});

const result3 = await schema2.checkAsync({ str: '12' });

expect(result3).to.deep.equal({
str: { hasError: true, errorMessage: 'error' }
});
});
});
});

describe('required', () => {
Expand Down
62 changes: 62 additions & 0 deletions test/ObjectTypeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,66 @@ describe('#ObjectType', () => {
expect(result.user.object.email1.errorMessage).to.equal('email1 is a required field');
expect(result.user.object.email2.errorMessage).to.equal('Email is required');
});

describe('priority', () => {
it('Should have the correct priority', () => {
const schema = new Schema({
user: ObjectType().shape({
name: StringType()
.isEmail('error1')
.addRule(() => false, 'error2')
})
});

const result = schema.check({ user: { name: 'a' } });

expect(result.user.object).to.deep.equal({
name: { hasError: true, errorMessage: 'error1' }
});

const schema2 = new Schema({
user: ObjectType().shape({
name: StringType()
.isEmail('error1')
.addRule(() => false, 'error2', true)
})
});

const result2 = schema2.check({ user: { name: 'a' } });

expect(result2.user.object).to.deep.equal({
name: { hasError: true, errorMessage: 'error2' }
});
});

it('Should have the correct priority with async', async () => {
const schema = new Schema({
user: ObjectType().shape({
name: StringType()
.isEmail('error1')
.addRule(() => false, 'error2')
})
});

const result = await schema.checkAsync({ user: { name: 'a' } });

expect(result.user.object).to.deep.equal({
name: { hasError: true, errorMessage: 'error1' }
});

const schema2 = new Schema({
user: ObjectType().shape({
name: StringType()
.isEmail('error1')
.addRule(() => false, 'error2', true)
})
});

const result2 = await schema2.checkAsync({ user: { name: 'a' } });

expect(result2.user.object).to.deep.equal({
name: { hasError: true, errorMessage: 'error2' }
});
});
});
});

0 comments on commit 176e04a

Please sign in to comment.