From 176e04a7f6caa3c4f7fd482ad6f461c37bd9ede6 Mon Sep 17 00:00:00 2001 From: simonguo Date: Fri, 12 Apr 2024 11:14:17 +0800 Subject: [PATCH] test: add tests for rule priority --- src/MixedType.ts | 20 +++++++------ test/MixedTypeSpec.js | 68 ++++++++++++++++++++++++++++++++++++++++++ test/ObjectTypeSpec.js | 62 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 9 deletions(-) diff --git a/src/MixedType.ts b/src/MixedType.ts index d91cae5..fc50d9d 100644 --- a/src/MixedType.ts +++ b/src/MixedType.ts @@ -89,10 +89,11 @@ export class MixedType validator(value, this.priorityRules) - .then((checkStatus: CheckResult | void | null) => { - if (checkStatus) { - resolve(checkStatus); + .then((checkResult: CheckResult | void | null) => { + // If the priority rule fails, return the result directly + if (checkResult) { + resolve(checkResult); } }) .then(() => { @@ -135,9 +137,9 @@ export class MixedType validator(value, this.rules)) - .then((checkStatus: CheckResult | void | null) => { - if (checkStatus) { - resolve(checkStatus); + .then((checkResult: CheckResult | void | null) => { + if (checkResult) { + resolve(checkResult); } resolve({ hasError: false }); }) diff --git a/test/MixedTypeSpec.js b/test/MixedTypeSpec.js index 83beaa2..8d10caf 100644 --- a/test/MixedTypeSpec.js +++ b/test/MixedTypeSpec.js @@ -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', () => { diff --git a/test/ObjectTypeSpec.js b/test/ObjectTypeSpec.js index 4f77d4f..375709f 100644 --- a/test/ObjectTypeSpec.js +++ b/test/ObjectTypeSpec.js @@ -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' } + }); + }); + }); });