-
Notifications
You must be signed in to change notification settings - Fork 174
/
temp.js
126 lines (121 loc) · 2.65 KB
/
temp.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Node.js require:
const Ajv = require('ajv');
// const ajv = new Ajv({ allErrors: true, jsonPointers: true });
// require('ajv-errors')(ajv);
const ajv = new Ajv({ allErrors: true });
const schema = {
type: 'object',
required: ['name'],
properties: {
name: {
type: 'string',
title: 'First name',
'ui:control': 'input',
maxLength: 5
},
brother: {
type: 'object',
properties: {
foo: {
type: 'string',
title: 'First name',
'ui:control': 'input'
},
bar: {
type: 'number',
title: 'First name',
'ui:control': 'input',
'input:props': {
type: 'number'
}
}
}
},
age: {
type: 'number',
title: 'First name',
'ui:control': 'input',
'input:props': {
type: 'number'
}
},
bio: {
type: 'string',
title: 'Bio',
'ui:control': 'textarea'
},
authorize: {
type: 'string',
title: 'Authorize',
'ui:control': 'checkbox',
errorMessage: {
type: 'Invalid format'
}
},
color: {
type: 'string',
title: 'Color',
'ui:control': 'select',
oneOf: [
{
const: '',
title: '- Select -',
'input:props': {
disabled: true
}
},
{ const: 'red', title: 'Red' },
{ const: 'black', title: 'Black' },
{ const: 'white', title: 'White' }
]
},
model: {
type: 'string',
title: 'Model',
'ui:control': 'radio',
oneOf: [
{ const: 'ms', title: 'Model S' },
{ const: 'm3', title: 'Model 3' },
{ const: 'mx', title: 'Model X' },
{ const: 'my', title: 'Model Y' }
],
default: null,
'informed:props': {
initialValue: 'm3'
}
},
cars: {
type: 'array',
title: 'Cars',
'ui:control': 'select',
'input:props': {
multiple: true,
style: { height: '100px', width: '200px' }
},
items: {
oneOf: [
{ const: 'tesla', title: 'Tesla' },
{ const: 'volvo', title: 'Volvo' },
{ const: 'audi', title: 'Audi' },
{ const: 'jeep', title: 'Jeep' }
]
},
'informed:props': {
initialValue: ['jeep', 'tesla']
}
}
}
};
const data = {
model: 'm3',
cars: ['jeep', 'tesla'],
// name: 'Joe Puzzo',
age: 26,
bio: 'Hello',
authorize: true,
color: 'red'
};
const validate = ajv.compile(schema);
const valid = validate(data);
console.log(validate.errors);
console.log('KEYS', Object.keys(schema.properties));