Skip to content

Commit cba26eb

Browse files
author
Rodrigo Solis
committed
fix: sanitize first -> validate second, makes sanitizing optional
1 parent 8b2ac71 commit cba26eb

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

examples/contact.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,5 @@ function proxyTestDrive(name) {
5252
}
5353
}
5454
proxyTestDrive('Mike');
55-
//
55+
proxyTestDrive(' JENNY');
5656
proxyTestDrive(' MICHAEL');
57-
// success! { name: 'MICHAEL' };

src/proxy.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@ import type { ProxyFactory } from './types';
77
export default function ProxyValidator(schema: Object, sanitizersSchema: Object): ProxyFactory {
88
const handler = {
99
set(object: Object, prop: string, value: string): boolean {
10+
let sanitizedValue = value;
11+
if (sanitizersSchema) {
12+
const { [prop]: sanitizers } = sanitizersSchema;
13+
// save sanitized value for later validation
14+
sanitizedValue = applySanitizers(sanitizers, value);
15+
}
16+
17+
// apply validation rules on sanitizedValue
1018
const { [prop]: rules } = schema;
11-
const { success, errors } = checkRules(rules, value);
19+
const { success, errors } = checkRules(rules, sanitizedValue);
1220
if (success) {
13-
const { [prop]: sanitizers } = sanitizersSchema;
14-
object[prop] = applySanitizers(sanitizers, value);
21+
object[prop] = sanitizedValue;
1522
return success;
1623
}
24+
1725
throw new ValidationError({ [prop]: errors });
1826
}
1927
};

src/validation.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { ValidationRules, SanitizingRules } from './types';
55

66
const GENERIC_ERROR_MESSAGE = 'Invalid input';
77

8-
export function checkRules(rules: ValidationRules = {}, value: string) {
8+
export function checkRules(rules: ValidationRules = {}, value: mixed) {
99
const errors = [];
1010
const success = Object.keys(rules)
1111
.reduce((acc, rule) => {

0 commit comments

Comments
 (0)