arstotzka 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +37 -1
- package/package.json +1 -1
- package/readme.md +19 -6
- package/usage.js +7 -1
package/index.js
CHANGED
|
@@ -7,7 +7,8 @@ export const ERRORS = {
|
|
|
7
7
|
notArray: "Tried using ARRAY_OF constraint on non-array value",
|
|
8
8
|
targetIsNull: "Passed object or array item is null",
|
|
9
9
|
functionExpected: "Expected function as dynamic constraint",
|
|
10
|
-
objectExpected: "Expected object"
|
|
10
|
+
objectExpected: "Expected object",
|
|
11
|
+
anyFailed: "None of ANY_OF constraints are met"
|
|
11
12
|
};
|
|
12
13
|
|
|
13
14
|
export const OPTIONAL = Symbol();
|
|
@@ -17,6 +18,18 @@ export function ARRAY_OF(constraints){
|
|
|
17
18
|
}
|
|
18
19
|
return constraint(FC_ARRAY, null, constraints);
|
|
19
20
|
}
|
|
21
|
+
export function ANY_OF(constraints){
|
|
22
|
+
if (arguments.length > 1){
|
|
23
|
+
return constraint(FC_ANY, null, Array.from(arguments));
|
|
24
|
+
} else {
|
|
25
|
+
if (Array.isArray(constraints)){
|
|
26
|
+
return constraint(FC_ANY, null, constraints);
|
|
27
|
+
} else {
|
|
28
|
+
return constraint(FC_ANY, null, [constraints]);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
}
|
|
20
33
|
export function DYNAMIC(constraints){
|
|
21
34
|
return constraint(FC_DYNAMIC, null, constraints);
|
|
22
35
|
}
|
|
@@ -25,6 +38,7 @@ const TYPE = t => x => typeof x == t;
|
|
|
25
38
|
|
|
26
39
|
const CONSTRAINT = Symbol();
|
|
27
40
|
const FC_ARRAY = Symbol(); // https://www.youtube.com/watch?v=qSqXGeJJBaI
|
|
41
|
+
const FC_ANY = Symbol();
|
|
28
42
|
const FC_DYNAMIC = Symbol();
|
|
29
43
|
const FC_NESTED = Symbol();
|
|
30
44
|
|
|
@@ -159,6 +173,28 @@ function checkValue(propertyName, value, constraints, options){
|
|
|
159
173
|
|
|
160
174
|
break;
|
|
161
175
|
}
|
|
176
|
+
case FC_ANY: {
|
|
177
|
+
const subSchemas = constraint.expected;
|
|
178
|
+
|
|
179
|
+
const subErrors = [];
|
|
180
|
+
let passed = false;
|
|
181
|
+
let counter = 0;
|
|
182
|
+
|
|
183
|
+
for (let subSchema of subSchemas){
|
|
184
|
+
const [subConstraints, flags] = parseSchema(subSchema);
|
|
185
|
+
const caseErrors = checkValue(`${propertyName}.<any#${counter}>`, value, subConstraints, options);
|
|
186
|
+
++counter;
|
|
187
|
+
subErrors.push(caseErrors);
|
|
188
|
+
if (caseErrors.length == 0) {
|
|
189
|
+
passed = true;
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (!passed) errors.push(error(propertyName, "anyFailed", undefined, subErrors));
|
|
195
|
+
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
162
198
|
case FC_DYNAMIC: {
|
|
163
199
|
const constraintCallback = constraint.expected;
|
|
164
200
|
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -32,12 +32,17 @@ console.log(Arstotzka.validate(goodData, schema, {allowExtraProperties: false}))
|
|
|
32
32
|
|
|
33
33
|
### Import:
|
|
34
34
|
Arstotzka is an ES6 module, so:
|
|
35
|
-
|
|
35
|
+
```
|
|
36
|
+
import * as Arstotzka from "arstotzka";
|
|
37
|
+
```
|
|
36
38
|
or, if you are okay with polluting namespace:
|
|
37
|
-
|
|
39
|
+
```
|
|
40
|
+
import { validate, OPTIONAL, ARRAY_OF, ANY_OF, DYNAMIC } from "arstotzka";
|
|
41
|
+
```
|
|
42
|
+
|
|
38
43
|
|
|
39
44
|
### Schema format:
|
|
40
|
-
Schema is a value that can be
|
|
45
|
+
Schema is a value that can be either of
|
|
41
46
|
- **string**: such schema will make validator check coresponding value's type with [typeof operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof), with exception of "array" constraint -- validator will [treat this type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray) as special case;
|
|
42
47
|
`"string"`, `"number"`, `"array"`
|
|
43
48
|
|
|
@@ -51,17 +56,24 @@ Schema is a value that can be a
|
|
|
51
56
|
`["string", x => x != x.trim()]`, `["number", x => x >= 0, x => x % 1 === 0]`
|
|
52
57
|
|
|
53
58
|
- **Arstotzka.ARRAY_OF()**: the function accepts any of above and returns a special constraint appliable to an array of values;
|
|
54
|
-
|
|
59
|
+
`Arstotzka.ARRAY_OF("number")`, `Arstotzka.ARRAY_OF({id: "number", text: "string"})`, `Arstotzka.ARRAY_OF(["number", x => x > 0])`, `Arstotzka.ARRAY_OF(Arstotzka.ARRAY_OF("number"))`
|
|
60
|
+
|
|
61
|
+
- **Arstotzka.ANY_OF()**: the function accepts array or vararg of schemas and returns a special constraint, which will produce a specific error only if every provided schema is violated;
|
|
62
|
+
`Arstotzka.ANY_OF(["number", ["string", x => !isNaN(parseInt(x))]])`
|
|
55
63
|
|
|
56
64
|
- **Arstotzka.DYNAMIC()**: the function accepts a callback that should return a valid schema, allowing to define schema at runtime;
|
|
57
|
-
|
|
65
|
+
`Arstotzka.DYNAMIC(x => dynSchema[x.type])`
|
|
66
|
+
|
|
67
|
+
- **Arstotzka.OPTIONAL**: unlike others, this schema doesn't imply any requirements, but prevents validator from logging an error in case it's property is not present in target object;
|
|
68
|
+
|
|
69
|
+
Applying a schema to a property that is an object can be done by combining **object** schema with anything via **array** schema;
|
|
58
70
|
|
|
59
|
-
Applying a schema to a property that is an object can be done by combining **object** schema with anything via **array** schema:
|
|
60
71
|
|
|
61
72
|
### Available options:
|
|
62
73
|
- **allErrors** (default is `true`) : If false, will return errors as soon as encountered, interrupting validation
|
|
63
74
|
- **allowExtraProperties** (default is `true`) : If false, adds specific error to a list for every property of target object not present in schema
|
|
64
75
|
|
|
76
|
+
|
|
65
77
|
### Error format
|
|
66
78
|
```
|
|
67
79
|
{ // Example error item:
|
|
@@ -75,5 +87,6 @@ Applying a schema to a property that is an object can be done by combining **obj
|
|
|
75
87
|
|
|
76
88
|
All error ids and messages can be found at `Arstotzka.ERRORS`
|
|
77
89
|
|
|
90
|
+
|
|
78
91
|
## License
|
|
79
92
|
Shared under WTFPL license.
|
package/usage.js
CHANGED
|
@@ -91,13 +91,19 @@ const testSubject2 = {
|
|
|
91
91
|
zero: "0"
|
|
92
92
|
};
|
|
93
93
|
|
|
94
|
+
const parseableIntSchema = Arstotzka.ANY_OF(
|
|
95
|
+
"number",
|
|
96
|
+
["string", x => !isNaN(parseInt(x))]
|
|
97
|
+
);
|
|
98
|
+
|
|
94
99
|
const tests = [
|
|
95
100
|
[null, {x: "number"}],
|
|
96
101
|
[testSubject0, schema], // Only an error caused by invalid validator
|
|
97
102
|
[testSubject1, schema], // Full of errors
|
|
98
103
|
["miles", "string"], // Any value can be validated, not only objects
|
|
99
104
|
[7, "string"],
|
|
100
|
-
[testSubject2, schema1]
|
|
105
|
+
[testSubject2, schema1],
|
|
106
|
+
["7", parseableIntSchema]
|
|
101
107
|
];
|
|
102
108
|
|
|
103
109
|
const TEST_SELECTOR = 0;
|