-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple-demo.js
44 lines (40 loc) · 1.05 KB
/
simple-demo.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
import trim from 'jq-trim';
import { createModel } from './src/FormModel';
const model = createModel({
descriptors: {
name: {
required: 'Name is required',
},
// validator for email
email: {
// validator function
async validator(field) {
const email = trim(field.value);
// super simple and naive email validation
if (!email || !(email.indexOf('@') > 0)) {
throw new Error('Please provide an error message');
}
},
},
// validator for password
password: {
// validator function
async validator(field) {
if (!trim(field.value)) {
throw new Error('Please provide your password');
}
},
},
},
initialState: {
email: '',
password: '',
},
});
const main = async () => {
await model.validate();
console.log('>>>> model.valid', model.valid);
console.log('>>>> model.summary', model.summary);
console.log('>>>> model.requiredFields', model.requiredFields);
};
main().catch(err => console.error('>>> error', err));