UNPKG

1.05 kBJavaScriptView Raw
1function validate(schema, slaterc) {
2 const tests = [isValidType];
3 let errors = [];
4 let warnings = [];
5
6 tests.forEach(test => {
7 const results = test(schema, slaterc);
8 errors = errors.concat(results.errors);
9 warnings = warnings.concat(results.warnings);
10 });
11
12 return {
13 errors,
14 warnings,
15 isValid: errors.length === 0,
16 };
17}
18
19function extractType(item, value) {
20 if (item.type === `array` && Array.isArray(value)) {
21 return item.type;
22 }
23
24 return typeof value;
25}
26
27function isValidType(schema, slaterc) {
28 const errors = [];
29 const warnings = [];
30
31 schema.items.forEach(item => {
32 const key = item.id;
33 const value = slaterc[key];
34 const type = extractType(item, value);
35
36 if (type !== 'undefined') {
37 if (type !== item.type) {
38 errors.push(
39 `Setting ${key}: Expected type '${
40 item.type
41 }' but received type '${type}'`,
42 );
43 }
44 }
45 });
46
47 return {errors, warnings};
48}
49
50module.exports = validate;
51module.exports.tests = {
52 isValidType,
53};