Skip to content
Myr

test(validator): add test for custom rules (#620)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* test(validator): add test for custom rules * fixed value

authored by Yusuke Wada · committed by GitHub · parent 8627010 · Browse files · 3 files+43 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+43 added−0 removed

  • tests:+41 added−0 removed
  • other:+2 added−0 removed

3 files+43 added−0 removed

modified:deno_dist/validator/validator.ts+1 added
254 unmodified lines
255 if (typeRule) {256 const typeResult = this.validateRule(typeRule, value)257 typeResult.jsonData ||= jsonData258 results.unshift(typeResult)259 this.rules.unshift(typeRule)260 }261262 return results263 }
230 unmodified lines
modified:src/validator/validator.test.ts+41 added
728 unmodified lines
729 expect(results[0].isValid).toBe(false)730 expect(results[0].message).toBe([customMessage].join('\n'))731 })732})733734describe('Custom rule', () => {735 const v = new Validator()736 const validator = v.body('screenName').addRule('should be screen name', (value) => {737 if (typeof value === 'string') {738 return /^[0-9a-zA-Z\_]+$/.test(value)739 }740 return false741 })742743 it('Should be valid - custom rule', async () => {744 const body = new FormData()745 body.append('screenName', 'honojs_honojs_honojs_honojs_honojs')746747 const req = new Request('http://localhost/', {748 method: 'POST',749 body: body,750 })751 const results = await validator.validate(req)752 expect(results[0].isValid).toBe(true)753 expect(results[1].isValid).toBe(true)754 })755756 it('Should be invalid - custom rule', async () => {757 const body = new FormData()758 body.append('screenName', 'honojs+honojs')759760 const req = new Request('http://localhost/', {761 method: 'POST',762 body: body,763 })764 const results = await validator.validate(req)765 expect(results[0].isValid).toBe(true)766 expect(results[1].isValid).toBe(false)767 expect(results[1].message).toBe(768 [769 'Invalid Value [honojs+honojs]: the request body "screenName" is invalid - should be screen name',770 ].join('\n')771 )772 })773})
modified:src/validator/validator.ts+1 added
254 unmodified lines
255 if (typeRule) {256 const typeResult = this.validateRule(typeRule, value)257 typeResult.jsonData ||= jsonData258 results.unshift(typeResult)259 this.rules.unshift(typeRule)260 }261262 return results263 }
230 unmodified lines