arstotzka 0.12.0 → 0.12.1
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/package.json +1 -1
- package/readme.md +14 -6
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, 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,19 @@ 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"))`
|
|
55
60
|
|
|
56
61
|
- **Arstotzka.DYNAMIC()**: the function accepts a callback that should return a valid schema, allowing to define schema at runtime;
|
|
57
|
-
|
|
62
|
+
`Arstotzka.DYNAMIC(x => dynSchema[x.type])`
|
|
63
|
+
|
|
64
|
+
Applying a schema to a property that is an object can be done by combining **object** schema with anything via **array** schema;
|
|
58
65
|
|
|
59
|
-
Applying a schema to a property that is an object can be done by combining **object** schema with anything via **array** schema:
|
|
60
66
|
|
|
61
67
|
### Available options:
|
|
62
68
|
- **allErrors** (default is `true`) : If false, will return errors as soon as encountered, interrupting validation
|
|
63
69
|
- **allowExtraProperties** (default is `true`) : If false, adds specific error to a list for every property of target object not present in schema
|
|
64
70
|
|
|
71
|
+
|
|
65
72
|
### Error format
|
|
66
73
|
```
|
|
67
74
|
{ // Example error item:
|
|
@@ -75,5 +82,6 @@ Applying a schema to a property that is an object can be done by combining **obj
|
|
|
75
82
|
|
|
76
83
|
All error ids and messages can be found at `Arstotzka.ERRORS`
|
|
77
84
|
|
|
85
|
+
|
|
78
86
|
## License
|
|
79
87
|
Shared under WTFPL license.
|