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
Provenance
- Commit
e92dbe9- Tree
ca3ff8d- Author
Jayden <yujonglee@g.skku.edu>- Committer
GitHub <noreply@github.com>- Parent
d3a6f80
3 files changed+58 added−16 removed
modified:deno_dist/validator/validator.ts+9 added-8 removed
240 unmodified lines241 value = JSONPathCopy(obj, dst, this.key)242 if (this._nested()) jsonData = dst243 }244 245+ value = this.sanitizeValue(value)246+247 const results: ValidateResult[] = []248 249 let typeRule = this.rules.shift()250 21 unmodified lines272 const prefix = 'should be'273 return this.isArray ? `${prefix} "${this.type}[]"` : `${prefix} "${this.type}"`274 }275 276+ private sanitizeValue = (value: Type) => (277+ this.sanitizers.reduce(278+ (acc, sanitizer) => sanitizer(acc),279+ value280+ )281+ )282+283 private validateRule(rule: Rule, value: Type): ValidateResult {284 let isValid: boolean = false285 286 if (this._nested() && this.target != 'json') {53 unmodified lines340 if (this._optional && typeof value === 'undefined') return true341 342 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 true148 unmodified linesmodified:src/validator/validator.test.ts+40 added
813 unmodified lines814 ].join('\n')815 )816 })817 })818+819+describe('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+ })837+838+ test('custom', async () => {839+ const v = new Validator()840+841+ const post = { name: ' a bc ' }842+843+ const req = new Request('http://localhost/', {844+ method: 'POST',845+ body: JSON.stringify(post),846+ })847+848+ const validator = v849+ .json('name')850+ .addSanitizer((s: unknown) => `${s}`.replace(/\s/g, ''))851+ .addSanitizer((s: unknown) => `${s}`.toUpperCase())852+853+ const [results] = await validator.validate(req)854+855+ expect(results.value).toBe('ABC')856+ })857+})modified:src/validator/validator.ts+9 added-8 removed
240 unmodified lines241 value = JSONPathCopy(obj, dst, this.key)242 if (this._nested()) jsonData = dst243 }244 245+ value = this.sanitizeValue(value)246+247 const results: ValidateResult[] = []248 249 let typeRule = this.rules.shift()250 21 unmodified lines272 const prefix = 'should be'273 return this.isArray ? `${prefix} "${this.type}[]"` : `${prefix} "${this.type}"`274 }275 276+ private sanitizeValue = (value: Type) => (277+ this.sanitizers.reduce(278+ (acc, sanitizer) => sanitizer(acc),279+ value280+ )281+ )282+283 private validateRule(rule: Rule, value: Type): ValidateResult {284 let isValid: boolean = false285 286 if (this._nested() && this.target != 'json') {53 unmodified lines340 if (this._optional && typeof value === 'undefined') return true341 342 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 true148 unmodified lines