Skip to content
Myr

fix(sanitizer) (#661)

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

* Add test for #660 * Implement sanitizeValue * Mark sanitizeValue private

authored by Jayden · committed by GitHub · parent d3a6f80 · Browse files · 3 files+58 added−16 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+58 added−16 removed

  • tests:+40 added−0 removed
  • other:+18 added−16 removed

3 files+58 added−16 removed

modified:deno_dist/validator/validator.ts+9 added-8 removed
240 unmodified lines
241 value = JSONPathCopy(obj, dst, this.key)242 if (this._nested()) jsonData = dst243 }244245 value = this.sanitizeValue(value)246247 const results: ValidateResult[] = []248249 let typeRule = this.rules.shift()250
21 unmodified lines
272 const prefix = 'should be'273 return this.isArray ? `${prefix} "${this.type}[]"` : `${prefix} "${this.type}"`274 }275276 private sanitizeValue = (value: Type) => (277 this.sanitizers.reduce(278 (acc, sanitizer) => sanitizer(acc),279 value280 )281 )282283 private validateRule(rule: Rule, value: Type): ValidateResult {284 let isValid: boolean = false285286 if (this._nested() && this.target != 'json') {
53 unmodified lines
340 if (this._optional && typeof value === 'undefined') return true341342 if (Array.isArray(value)) {343 if (value.length === 0 && !this._optional) return false335 // Sanitize336 for (const sanitizer of this.sanitizers) {337 value = value.map((innerVal: any) => sanitizer(innerVal)) as JSONArray338 }344 for (const val of value) {345 if (!func(val)) {346 return false347 }348 }349 return true350 } else {346 // Sanitize347 for (const sanitizer of this.sanitizers) {348 value = sanitizer(value)349 }351 if (!func(value)) {352 return false353 }354 return true
148 unmodified lines
modified:src/validator/validator.test.ts+40 added
813 unmodified lines
814 ].join('\n')815 )816 })817})818819describe('Sanitizer', () => {820 describe('built-in', () => {821 test('trim', async () => {822 const v = new Validator()823 824 const post = { name: ' a bc ' }825 826 const req = new Request('http://localhost/', {827 method: 'POST',828 body: JSON.stringify(post),829 })830 831 const validator = v.json('name').trim()832 const [result] = await validator.validate(req)833 834 expect(result.value).toBe('a bc')835 })836 })837838 test('custom', async () => {839 const v = new Validator()840841 const post = { name: ' a bc ' }842843 const req = new Request('http://localhost/', {844 method: 'POST',845 body: JSON.stringify(post),846 })847848 const validator = v849 .json('name')850 .addSanitizer((s: unknown) => `${s}`.replace(/\s/g, ''))851 .addSanitizer((s: unknown) => `${s}`.toUpperCase())852853 const [results] = await validator.validate(req)854855 expect(results.value).toBe('ABC')856 })857})
modified:src/validator/validator.ts+9 added-8 removed
240 unmodified lines
241 value = JSONPathCopy(obj, dst, this.key)242 if (this._nested()) jsonData = dst243 }244245 value = this.sanitizeValue(value)246247 const results: ValidateResult[] = []248249 let typeRule = this.rules.shift()250
21 unmodified lines
272 const prefix = 'should be'273 return this.isArray ? `${prefix} "${this.type}[]"` : `${prefix} "${this.type}"`274 }275276 private sanitizeValue = (value: Type) => (277 this.sanitizers.reduce(278 (acc, sanitizer) => sanitizer(acc),279 value280 )281 )282283 private validateRule(rule: Rule, value: Type): ValidateResult {284 let isValid: boolean = false285286 if (this._nested() && this.target != 'json') {
53 unmodified lines
340 if (this._optional && typeof value === 'undefined') return true341342 if (Array.isArray(value)) {343 if (value.length === 0 && !this._optional) return false335 // Sanitize336 for (const sanitizer of this.sanitizers) {337 value = value.map((innerVal: any) => sanitizer(innerVal)) as JSONArray338 }344 for (const val of value) {345 if (!func(val)) {346 return false347 }348 }349 return true350 } else {346 // Sanitize347 for (const sanitizer of this.sanitizers) {348 value = sanitizer(value)349 }351 if (!func(value)) {352 return false353 }354 return true
148 unmodified lines