-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
53 lines (43 loc) · 1.2 KB
/
example.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
let validator = require('./validator')
// Example 1 generalized
let raw1 = {
a: 'string-value', // string
b: 0, // number
c: true, // boolean
d: undefined, // undefined
e: null, // object
f: [], // object
g: {}, // object
h: () => {} // function
}
let defaults1 = {
a: {
required: true, // required (boolean of required) is by default true
type: 'string', // type (string of type) is by default required to be specified
validate: (input) => { return true }, // validate (validate of function) is by default not required
default: 'a' // default (object) is by default required when specifing object data
}
}
// takes raw json,
// takes default template
// and optional boolean if unspecified params in raw remove else include
let result1 = validator(raw1, defaults1, true)
console.log(result1)
// Example 2 use case
let raw2 = {
name: 'Kevin',
age: 21
}
let defaults2 = {
name: {
type: 'string',
default: 'no_name_provided'
},
age: {
type: 'number',
validate: (age) => { return age > 0 },
default: 'no_age_provided'
}
}
let result2 = validator(raw2, defaults2)
console.log(result2)