Skip to content
Myr

fix(validator): make "isOptional" work correctly with "value" validation (#613)

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

authored by Yusuke Wada · committed by GitHub · parent 47b5478 · Browse files · 3 files+17 added−6 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+17 added−6 removed

  • tests:+13 added−6 removed
  • other:+4 added−0 removed

3 files+17 added−6 removed

modified:deno_dist/middleware/validator/validator.ts+2 added
321 unmodified lines
322 return true323 }324325 private validateValue = (func: (value: Type) => boolean, value: Type): boolean => {326 if (this._optional && typeof value === 'undefined') return true327328 if (Array.isArray(value)) {329 // Sanitize330 for (const sanitizer of this.sanitizers) {331 value = value.map((innerVal: any) => sanitizer(innerVal)) as JSONArray
161 unmodified lines
modified:src/middleware/validator/validator.test.ts+13 added-6 removed
182 unmodified lines
183 })184})185186describe('Handle optional values', () => {187 const v = new Validator()188189 const req = new Request('http://localhost/', {190 method: 'POST',191 })192187 it('Should be valid - `comment` is optional', async () => {188 const v = new Validator()189 const req = new Request('http://localhost/', {190 method: 'POST',191 })192 const validator = v.body('comment').isOptional()193 const results = await validator.validate(req)194 expect(results[0].isValid).toBe(true)195 })196 it('Should be valid - "isNumeric" but "isOptional"', async () => {197 const v = new Validator()198 const validator = v.query('page').isNumeric().isOptional()199 const req = new Request('http://localhost/')200 const results = await validator.validate(req)201 expect(results[0].isValid).toBe(true)202 expect(results[1].isValid).toBe(true)203 expect(results[1].message).toBeUndefined()204 })205})206207describe('Handling types error', () => {208 const v = new Validator()
524 unmodified lines
modified:src/middleware/validator/validator.ts+2 added
321 unmodified lines
322 return true323 }324325 private validateValue = (func: (value: Type) => boolean, value: Type): boolean => {326 if (this._optional && typeof value === 'undefined') return true327328 if (Array.isArray(value)) {329 // Sanitize330 for (const sanitizer of this.sanitizers) {331 value = value.map((innerVal: any) => sanitizer(innerVal)) as JSONArray
161 unmodified lines