-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators.js
69 lines (63 loc) · 2.3 KB
/
validators.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// names of validations you can add to your schema and functions used to check that the field value
// is in the correct format. can add others like max. or greaterThan/lessThan for numbers.
const validationTable = {
type: (fieldValue, configValue) =>
fieldValue ? typeof fieldValue === configValue.name.toLowerCase() : true,
required: (fieldValue, configValue) =>
configValue ? !!fieldValue : false,
min: (fieldValue, configValue) =>
fieldValue ? fieldValue.length >= configValue : true,
custom: (fieldValue, configValue) =>
fieldValue ? configValue(fieldValue) : true
};
// validator class is used to run a fieldValue through all validators in validationTable
class Validator {
constructor(field, config) {
this.field = field;
this.config = config;
}
validate(obj) {
const fieldValue = obj[this.field];
// for each item in config ({ type: String, min: 10, ...})
return Object.entries(this.config)
.map(([validationName, [configValue, message]]) => {
// find the validator from the validation table
// validationName is type, min, ....
// configValue is String, 10, ...
// message is 'business name must be a string', 'length', ...
// fieldValue is the value of the field in the object 'hi there'
return validationTable[validationName](fieldValue, configValue) ? null : message;
})
// filter out nulls (those are validations that passed)
.filter(message => message);
}
}
class Schema {
constructor(schema) {
this.schema = schema;
// create a new validator for each field in the schema
this.validators = Object.entries(schema).map(([field, config]) => new Validator(field, config));
}
validate(obj) {
// run all validators against an object
return this.validators.flatMap(validator => validator.validate(obj));
}
}
const businessSchema = new Schema({
name: {
type: [String, 'business name must be a string'],
required: [true, 'is required'],
min: [10, 'length']
},
email: {
type: [String, 'emails must be a string'],
custom: [email => email.includes('@'), 'invalid email: must include an @']
}
});
const validateBusiness = newBusiness => {
return businessSchema.validate(newBusiness);
};
console.log(businessSchema.validate({
name: 'hi there',
email: 'ryan'
}));