git ssb

1+

clemo / helperjs



Tree: fbad0279dd4439c8eb8da96d385e27acd5755c74

Files: fbad0279dd4439c8eb8da96d385e27acd5755c74 / lib / form.js

4087 bytesRaw
1import debug from 'debug';
2import validator from 'validator';
3import faker from 'faker';
4
5const log = debug('helper:form:info');
6
7export class Form {
8 constructor(validation) { this.validation = validation; }
9 fill(object, check = true) {
10 for (let element in this.validation) {
11 if ('undefined' !== typeof object && object[element]) {
12 this.validation[element].value = object[element];
13 }
14 if (check) {
15 this.validation[element].error = !this.isValid(element);
16 }
17 }
18 return this.validation;
19 } // end fill
20 isValid(element) {
21 if ("undefined" !== typeof element) { // check 1 element
22 if ("function" === typeof this.validation[element].convert) { // convert
23 this.validation[element].checkValue =
24 this.validation[element].convert(this.validation[element].value);
25 log("convert", element, this.validation[element].value, 'to',
26 this.validation[element].checkValue);
27 } else if ("string" === typeof this.validation[element].convert) {
28 log("convert", element, this.validation[element].value, 'to',
29 this.validation[element].checkValue);
30 this.validation[element].checkValue =
31 validator[this.validation[element].convert](
32 this.validation[element].value);
33 } else {
34 this.validation[element].checkValue = this.validation[element].value;
35 }
36 // validate
37 log(typeof this.validation[element].validator,
38 typeof validator[this.validation[element].validator],
39 typeof this.validation[element].checkValue);
40
41 if ("string" === typeof this.validation[element].validator &&
42 "function" === typeof validator[this.validation[element].validator] &&
43 "string" ===
44 typeof this.validation[element]
45 .checkValue) { // validation function
46 log("validat by validator string", element);
47 return validator[this.validation[element].validator](
48 this.validation[element].checkValue,
49 this.validation[element].options);
50 } else if ("function" === typeof this.validation[element].validator &&
51 "string" ===
52 typeof this.validation[element]
53 .checkValue) { // own validation function
54 log("validat by validator function", element);
55 return this.validation[element].validator(
56 this.validation[element].checkValue);
57 } else if (this.validation[element].required &&
58 ("undefined" === typeof this.validation[element].checkValue ||
59 "" ===
60 this.validation[element]
61 .checkValue)) { // validation but no value
62 log("validat required but no value", element);
63 return false;
64 } else if ("undefined" === typeof this.validation[element].validator ||
65 !this.validation[element]
66 .required) { // no validation or not required
67 log("validat no validator (true)", element);
68 return true; // no validation, so valid
69 }
70 return false;
71
72 } else { // check everything
73 let valid = true;
74 for (let e in this.validation) {
75 if (!this.isValid(e)) {
76 valid = false;
77 }
78 }
79 return valid;
80 }
81 } // end isValid
82
83 fakeFill(element) {
84
85 if (element) {
86 if ('string' === typeof this.validation[element].fake) {
87 this.validation[element].value =
88 faker.fake(this.validation[element].fake);
89 log('fake fill', element, this.validation[element].value);
90 return true;
91 } else if ('function' === typeof this.validation[element].fake) {
92 let fake = this.validation[element].fake();
93
94 this.validation[element].value = faker.fake(fake);
95
96 return true;
97 }
98 return false;
99 } else {
100 let filled = true;
101 for (let e in this.validation) {
102 if (!this.fakeFill(e)) {
103 filled = false;
104 }
105 }
106 return filled;
107 }
108 }
109}
110
111exports.default = Form;
112

Built with git-ssb-web