mirror of
https://github.com/honojs/hono.git
synced 2024-12-01 11:51:01 +01:00
fix(sanitizer) (#661)
* Add test for #660 * Implement sanitizeValue * Mark sanitizeValue private
This commit is contained in:
parent
d3a6f80d7c
commit
e92dbe9558
@ -242,6 +242,8 @@ export abstract class VBase {
|
||||
if (this._nested()) jsonData = dst
|
||||
}
|
||||
|
||||
value = this.sanitizeValue(value)
|
||||
|
||||
const results: ValidateResult[] = []
|
||||
|
||||
let typeRule = this.rules.shift()
|
||||
@ -271,6 +273,13 @@ export abstract class VBase {
|
||||
return this.isArray ? `${prefix} "${this.type}[]"` : `${prefix} "${this.type}"`
|
||||
}
|
||||
|
||||
private sanitizeValue = (value: Type) => (
|
||||
this.sanitizers.reduce(
|
||||
(acc, sanitizer) => sanitizer(acc),
|
||||
value
|
||||
)
|
||||
)
|
||||
|
||||
private validateRule(rule: Rule, value: Type): ValidateResult {
|
||||
let isValid: boolean = false
|
||||
|
||||
@ -332,10 +341,6 @@ export abstract class VBase {
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
if (value.length === 0 && !this._optional) return false
|
||||
// Sanitize
|
||||
for (const sanitizer of this.sanitizers) {
|
||||
value = value.map((innerVal: any) => sanitizer(innerVal)) as JSONArray
|
||||
}
|
||||
for (const val of value) {
|
||||
if (!func(val)) {
|
||||
return false
|
||||
@ -343,10 +348,6 @@ export abstract class VBase {
|
||||
}
|
||||
return true
|
||||
} else {
|
||||
// Sanitize
|
||||
for (const sanitizer of this.sanitizers) {
|
||||
value = sanitizer(value)
|
||||
}
|
||||
if (!func(value)) {
|
||||
return false
|
||||
}
|
||||
|
@ -815,3 +815,43 @@ describe('Custom rule', () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Sanitizer', () => {
|
||||
describe('built-in', () => {
|
||||
test('trim', async () => {
|
||||
const v = new Validator()
|
||||
|
||||
const post = { name: ' a bc ' }
|
||||
|
||||
const req = new Request('http://localhost/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(post),
|
||||
})
|
||||
|
||||
const validator = v.json('name').trim()
|
||||
const [result] = await validator.validate(req)
|
||||
|
||||
expect(result.value).toBe('a bc')
|
||||
})
|
||||
})
|
||||
|
||||
test('custom', async () => {
|
||||
const v = new Validator()
|
||||
|
||||
const post = { name: ' a bc ' }
|
||||
|
||||
const req = new Request('http://localhost/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(post),
|
||||
})
|
||||
|
||||
const validator = v
|
||||
.json('name')
|
||||
.addSanitizer((s: unknown) => `${s}`.replace(/\s/g, ''))
|
||||
.addSanitizer((s: unknown) => `${s}`.toUpperCase())
|
||||
|
||||
const [results] = await validator.validate(req)
|
||||
|
||||
expect(results.value).toBe('ABC')
|
||||
})
|
||||
})
|
||||
|
@ -242,6 +242,8 @@ export abstract class VBase {
|
||||
if (this._nested()) jsonData = dst
|
||||
}
|
||||
|
||||
value = this.sanitizeValue(value)
|
||||
|
||||
const results: ValidateResult[] = []
|
||||
|
||||
let typeRule = this.rules.shift()
|
||||
@ -271,6 +273,13 @@ export abstract class VBase {
|
||||
return this.isArray ? `${prefix} "${this.type}[]"` : `${prefix} "${this.type}"`
|
||||
}
|
||||
|
||||
private sanitizeValue = (value: Type) => (
|
||||
this.sanitizers.reduce(
|
||||
(acc, sanitizer) => sanitizer(acc),
|
||||
value
|
||||
)
|
||||
)
|
||||
|
||||
private validateRule(rule: Rule, value: Type): ValidateResult {
|
||||
let isValid: boolean = false
|
||||
|
||||
@ -332,10 +341,6 @@ export abstract class VBase {
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
if (value.length === 0 && !this._optional) return false
|
||||
// Sanitize
|
||||
for (const sanitizer of this.sanitizers) {
|
||||
value = value.map((innerVal: any) => sanitizer(innerVal)) as JSONArray
|
||||
}
|
||||
for (const val of value) {
|
||||
if (!func(val)) {
|
||||
return false
|
||||
@ -343,10 +348,6 @@ export abstract class VBase {
|
||||
}
|
||||
return true
|
||||
} else {
|
||||
// Sanitize
|
||||
for (const sanitizer of this.sanitizers) {
|
||||
value = sanitizer(value)
|
||||
}
|
||||
if (!func(value)) {
|
||||
return false
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user