forked from icebob/fastest-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber.js
More file actions
138 lines (119 loc) · 3.03 KB
/
Copy pathnumber.js
File metadata and controls
138 lines (119 loc) · 3.03 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
"use strict";
/** Signature: function(value, field, parent, errors, context)
*/
module.exports = function({ schema, messages }, path, context) {
const src = [];
src.push(`
var origValue = value;
`);
let sanitized = false;
if (schema.convert === true) {
sanitized = true;
src.push(`
if (typeof value !== "number") {
value = Number(value);
}
`);
}
src.push(`
if (typeof value !== "number" || isNaN(value) || !isFinite(value)) {
${this.makeError({ type: "number", actual: "origValue", messages })}
return value;
}
`);
if (schema.min != null) {
src.push(`
if (value < ${schema.min}) {
${this.makeError({ type: "numberMin", expected: schema.min, actual: "origValue", messages })}
}
`);
}
if (schema.max != null) {
src.push(`
if (value > ${schema.max}) {
${this.makeError({ type: "numberMax", expected: schema.max, actual: "origValue", messages })}
}
`);
}
// Check fix value
if (schema.equal != null) {
src.push(`
if (value !== ${schema.equal}) {
${this.makeError({ type: "numberEqual", expected: schema.equal, actual: "origValue", messages })}
}
`);
}
// Check not fix value
if (schema.notEqual != null) {
src.push(`
if (value === ${schema.notEqual}) {
${this.makeError({ type: "numberNotEqual", expected: schema.notEqual, actual: "origValue", messages })}
}
`);
}
// Check integer
if (schema.integer === true) {
src.push(`
if (value % 1 !== 0) {
${this.makeError({ type: "numberInteger", actual: "origValue", messages })}
}
`);
}
// Check step
if (schema.step != null) {
if (schema.step <= 0 || !Number.isFinite(schema.step))
throw new Error(`Invalid '${schema.type}' schema. The 'step' field must be a positive number.`);
const errorSrc = this.makeError({ type: "numberStep", expected: schema.step, actual: "origValue", messages });
if (Number.isInteger(schema.step))
src.push(`
if (value % ${schema.step} !== 0) {
${errorSrc}
}
`)
else {
const stepDecimals = schema.step.toString().split('.')[1].length
const multiplier = Math.pow(10, stepDecimals);
const stepInt = Math.round(schema.step * multiplier);
src.push(`
if (!Number.isFinite(value)) {
${errorSrc}
} else {
const valStr = value.toString();
const valDotIdx = valStr.indexOf('.');
const valDecimals = valDotIdx !== -1 ? valStr.length - valDotIdx - 1 : 0;
if (valDecimals > ${stepDecimals}) {
${errorSrc}
} else {
const valInt = Math.round(value * ${multiplier});
if (valInt % ${stepInt} !== 0) {
${errorSrc}
}
}
}
`);
}
}
// Check positive
if (schema.positive === true) {
src.push(`
if (value <= 0) {
${this.makeError({ type: "numberPositive", actual: "origValue", messages })}
}
`);
}
// Check negative
if (schema.negative === true) {
src.push(`
if (value >= 0) {
${this.makeError({ type: "numberNegative", actual: "origValue", messages })}
}
`);
}
src.push(`
return value;
`);
return {
sanitized,
source: src.join("\n")
};
};