-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy patharray.js
116 lines (102 loc) · 2.72 KB
/
array.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
"use strict";
/** Signature: function(value, field, parent, errors, context)
*/
module.exports = function ({ schema, messages }, path, context) {
const src = [];
let sanitized = false;
if (schema.convert === true) {
sanitized = true;
// Convert to array if not and the value is not null or undefined
src.push(`
if (!Array.isArray(value) && value != null) {
value = [value];
}
`);
}
src.push(`
if (!Array.isArray(value)) {
${this.makeError({ type: "array", actual: "value", messages })}
return value;
}
var len = value.length;
`);
if (schema.empty === false) {
src.push(`
if (len === 0) {
${this.makeError({ type: "arrayEmpty", actual: "value", messages })}
}
`);
}
if (schema.min != null) {
src.push(`
if (len < ${schema.min}) {
${this.makeError({ type: "arrayMin", expected: schema.min, actual: "len", messages })}
}
`);
}
if (schema.max != null) {
src.push(`
if (len > ${schema.max}) {
${this.makeError({ type: "arrayMax", expected: schema.max, actual: "len", messages })}
}
`);
}
if (schema.length != null) {
src.push(`
if (len !== ${schema.length}) {
${this.makeError({ type: "arrayLength", expected: schema.length, actual: "len", messages })}
}
`);
}
if (schema.contains != null) {
src.push(`
if (value.indexOf(${JSON.stringify(schema.contains)}) === -1) {
${this.makeError({ type: "arrayContains", expected: JSON.stringify(schema.contains), actual: "value", messages })}
}
`);
}
if (schema.unique === true) {
src.push(`
if(len > (new Set(value)).size) {
${this.makeError({ type: "arrayUnique", expected: "Array.from(new Set(value.filter((item, index) => value.indexOf(item) !== index)))", actual: "value", messages })}
}
`);
}
if (schema.enum != null) {
const enumStr = JSON.stringify(schema.enum);
src.push(`
for (var i = 0; i < value.length; i++) {
if (${enumStr}.indexOf(value[i]) === -1) {
${this.makeError({ type: "arrayEnum", expected: "\"" + schema.enum.join(", ") + "\"", actual: "value[i]", messages })}
}
}
`);
}
if (schema.items != null) {
src.push(`
var arr = value;
var parentField = field;
for (var i = 0; i < arr.length; i++) {
value = arr[i];
`);
const itemPath = path + "[]";
const rule = this.getRuleFromSchema(schema.items);
// eslint-disable-next-line quotes
const innerSource = `arr[i] = ${context.async ? "await " : ""}context.fn[%%INDEX%%](arr[i], (parentField ? parentField : "") + "[" + i + "]", parent, errors, context)`;
src.push(this.compileRule(rule, context, itemPath, innerSource, "arr[i]"));
src.push(`
}
`);
src.push(`
return arr;
`);
} else {
src.push(`
return value;
`);
}
return {
sanitized,
source: src.join("\n")
};
};