arstotzka 0.14.1 → 0.14.3
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/license.md +14 -14
- package/package.json +4 -3
- package/readme.md +92 -92
package/license.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
|
2
|
-
Version 2, December 2004
|
|
3
|
-
|
|
4
|
-
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
|
|
5
|
-
|
|
6
|
-
Everyone is permitted to copy and distribute verbatim or modified
|
|
7
|
-
copies of this license document, and changing it is allowed as long
|
|
8
|
-
as the name is changed.
|
|
9
|
-
|
|
10
|
-
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
|
11
|
-
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
|
12
|
-
|
|
13
|
-
0. You just DO WHAT THE FUCK YOU WANT TO.
|
|
14
|
-
|
|
1
|
+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
|
2
|
+
Version 2, December 2004
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
|
|
5
|
+
|
|
6
|
+
Everyone is permitted to copy and distribute verbatim or modified
|
|
7
|
+
copies of this license document, and changing it is allowed as long
|
|
8
|
+
as the name is changed.
|
|
9
|
+
|
|
10
|
+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
|
11
|
+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
|
12
|
+
|
|
13
|
+
0. You just DO WHAT THE FUCK YOU WANT TO.
|
|
14
|
+
|
|
15
15
|
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "arstotzka",
|
|
3
|
-
"version": "0.14.
|
|
4
|
-
"
|
|
5
|
-
"main": "index.js",
|
|
3
|
+
"version": "0.14.3",
|
|
4
|
+
"private": false,
|
|
6
5
|
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": {
|
|
9
9
|
"main": "./dist/index.cjs",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"types": "./dist/index.d.ts"
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
|
+
"description": "JS validation utility",
|
|
14
15
|
"files": [
|
|
15
16
|
"dist"
|
|
16
17
|
],
|
package/readme.md
CHANGED
|
@@ -1,92 +1,92 @@
|
|
|
1
|
-
# Arstotzka
|
|
2
|
-
JS data validation tool featuring laconic schema format and a sexy name.
|
|
3
|
-
|
|
4
|
-
## Usage
|
|
5
|
-
|
|
6
|
-
See detailed usage example in **[usage.js](https://github.com/MilesVII/arstotzka/blob/master/usage.js)**
|
|
7
|
-
|
|
8
|
-
```
|
|
9
|
-
const schema = {
|
|
10
|
-
id: "number",
|
|
11
|
-
username: ["string", x => x.length < 10],
|
|
12
|
-
post: "string",
|
|
13
|
-
comments: Arstotzka.ARRAY_OF({
|
|
14
|
-
author: ["string", Arstotzka.OPTIONAL],
|
|
15
|
-
text: "string"
|
|
16
|
-
})
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const goodData = {
|
|
20
|
-
id: 1337,
|
|
21
|
-
username: "mr.hands",
|
|
22
|
-
post: "Henlo there",
|
|
23
|
-
comments: [
|
|
24
|
-
{author: "Johnny", text: "henlo"},
|
|
25
|
-
{text: "hey hey"},
|
|
26
|
-
{author: "miles", text: "birb"},
|
|
27
|
-
]
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
console.log(Arstotzka.validate(goodData, schema, {allowExtraProperties: false})); // Prints an array of errors
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
### Import:
|
|
34
|
-
Arstotzka is an ES6 module, so:
|
|
35
|
-
```
|
|
36
|
-
import * as Arstotzka from "arstotzka";
|
|
37
|
-
```
|
|
38
|
-
or, if you are okay with polluting namespace:
|
|
39
|
-
```
|
|
40
|
-
import { validate, OPTIONAL, ARRAY_OF, ANY_OF, DYNAMIC } from "arstotzka";
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
### Schema format:
|
|
45
|
-
Schema is a value that can be either of
|
|
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;
|
|
47
|
-
`"string"`, `"number"`, `"array"`
|
|
48
|
-
|
|
49
|
-
- **function**: such schema will make validator call it, pass corresponding value to it, and log an error if returned value is falsy;
|
|
50
|
-
`x => !isNaN(x)`, `x => x.length < 10`
|
|
51
|
-
|
|
52
|
-
- **object**: object schema requires corresponding value to also be an object, and will recursively match it's properties against provided value;
|
|
53
|
-
`{name: "string", age: x => x.length > 21}`
|
|
54
|
-
|
|
55
|
-
- **array**, which elements are any of above. That will require a value it matched against to fullfill *every* requirement;
|
|
56
|
-
`["string", x => x != x.trim()]`, `["number", x => x >= 0, x => x % 1 === 0]`
|
|
57
|
-
|
|
58
|
-
- **Arstotzka.ARRAY_OF()**: the function accepts any of above and returns a special constraint appliable to an array of values;
|
|
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))]])`
|
|
63
|
-
|
|
64
|
-
- **Arstotzka.DYNAMIC()**: the function accepts a callback that should return a valid schema, allowing to define schema at runtime;
|
|
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;
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
### Available options:
|
|
73
|
-
- **allErrors** (default is `true`) : If false, will return errors as soon as encountered, interrupting validation
|
|
74
|
-
- **allowExtraProperties** (default is `true`) : If false, adds specific error to a list for every property of target object not present in schema
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
### Error format
|
|
78
|
-
```
|
|
79
|
-
{ // Example error item:
|
|
80
|
-
propertyName: 'age', // Name of a property that failed the validation
|
|
81
|
-
id: 'typeMismatch', // String describing type of an error. Can be used to localize error message
|
|
82
|
-
message: 'Provided type is not allowed by schema', // Error message that coressponds to error id
|
|
83
|
-
expected: 'number', // Arbitrary-purpose fields
|
|
84
|
-
got: null
|
|
85
|
-
}
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
All error ids and messages can be found at `Arstotzka.ERRORS`
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
## License
|
|
92
|
-
Shared under WTFPL license.
|
|
1
|
+
# Arstotzka
|
|
2
|
+
JS data validation tool featuring laconic schema format and a sexy name.
|
|
3
|
+
|
|
4
|
+
## Usage
|
|
5
|
+
|
|
6
|
+
See detailed usage example in **[usage.js](https://github.com/MilesVII/arstotzka/blob/master/usage.js)**
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
const schema = {
|
|
10
|
+
id: "number",
|
|
11
|
+
username: ["string", x => x.length < 10],
|
|
12
|
+
post: "string",
|
|
13
|
+
comments: Arstotzka.ARRAY_OF({
|
|
14
|
+
author: ["string", Arstotzka.OPTIONAL],
|
|
15
|
+
text: "string"
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const goodData = {
|
|
20
|
+
id: 1337,
|
|
21
|
+
username: "mr.hands",
|
|
22
|
+
post: "Henlo there",
|
|
23
|
+
comments: [
|
|
24
|
+
{author: "Johnny", text: "henlo"},
|
|
25
|
+
{text: "hey hey"},
|
|
26
|
+
{author: "miles", text: "birb"},
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
console.log(Arstotzka.validate(goodData, schema, {allowExtraProperties: false})); // Prints an array of errors
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Import:
|
|
34
|
+
Arstotzka is an ES6 module, so:
|
|
35
|
+
```
|
|
36
|
+
import * as Arstotzka from "arstotzka";
|
|
37
|
+
```
|
|
38
|
+
or, if you are okay with polluting namespace:
|
|
39
|
+
```
|
|
40
|
+
import { validate, OPTIONAL, ARRAY_OF, ANY_OF, DYNAMIC } from "arstotzka";
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
### Schema format:
|
|
45
|
+
Schema is a value that can be either of
|
|
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;
|
|
47
|
+
`"string"`, `"number"`, `"array"`
|
|
48
|
+
|
|
49
|
+
- **function**: such schema will make validator call it, pass corresponding value to it, and log an error if returned value is falsy;
|
|
50
|
+
`x => !isNaN(x)`, `x => x.length < 10`
|
|
51
|
+
|
|
52
|
+
- **object**: object schema requires corresponding value to also be an object, and will recursively match it's properties against provided value;
|
|
53
|
+
`{name: "string", age: x => x.length > 21}`
|
|
54
|
+
|
|
55
|
+
- **array**, which elements are any of above. That will require a value it matched against to fullfill *every* requirement;
|
|
56
|
+
`["string", x => x != x.trim()]`, `["number", x => x >= 0, x => x % 1 === 0]`
|
|
57
|
+
|
|
58
|
+
- **Arstotzka.ARRAY_OF()**: the function accepts any of above and returns a special constraint appliable to an array of values;
|
|
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))]])`
|
|
63
|
+
|
|
64
|
+
- **Arstotzka.DYNAMIC()**: the function accepts a callback that should return a valid schema, allowing to define schema at runtime;
|
|
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;
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
### Available options:
|
|
73
|
+
- **allErrors** (default is `true`) : If false, will return errors as soon as encountered, interrupting validation
|
|
74
|
+
- **allowExtraProperties** (default is `true`) : If false, adds specific error to a list for every property of target object not present in schema
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
### Error format
|
|
78
|
+
```
|
|
79
|
+
{ // Example error item:
|
|
80
|
+
propertyName: 'age', // Name of a property that failed the validation
|
|
81
|
+
id: 'typeMismatch', // String describing type of an error. Can be used to localize error message
|
|
82
|
+
message: 'Provided type is not allowed by schema', // Error message that coressponds to error id
|
|
83
|
+
expected: 'number', // Arbitrary-purpose fields
|
|
84
|
+
got: null
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
All error ids and messages can be found at `Arstotzka.ERRORS`
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
Shared under WTFPL license.
|