Skip to content

Commit b77324b

Browse files
committed
Simplfy schemas by making implementation cast function arguments to array
1 parent 2310790 commit b77324b

21 files changed

+118
-67
lines changed

examples/All.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@
2828
"expression": { "All": [{ "Boolean": true }, { "Property": "admin" }] },
2929
"context": { "properties": { "admin": true } },
3030
"result": { "enum": [true] }
31+
},
32+
{
33+
"expression": { "All": null },
34+
"result": { "enum": [true] }
3135
}
3236
],
3337
"invalid": [
34-
{ "All": null },
3538
{ "All": [], "Any": [] }
3639
]
3740
}

examples/Any.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@
3232
"expression": { "Any": [{ "Boolean": false }, { "Property": "admin" }] },
3333
"context": { "properties": { "admin": true } },
3434
"result": { "enum": [true] }
35+
},
36+
{
37+
"expression": { "Any": null },
38+
"result": { "enum": [false] }
3539
}
3640
],
3741
"invalid": [
38-
{ "Any": null },
3942
{ "Any": [], "All": [] }
4043
]
4144
}

examples/Durations.json renamed to examples/Duration.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@
2727
{
2828
"expression": { "Duration": [2, "years"] },
2929
"result": { "enum": [63113904] }
30+
},
31+
{
32+
"expression": { "Duration": 2 },
33+
"result": { "enum": [2] }
34+
},
35+
{
36+
"expression": { "Duration": [2] },
37+
"result": { "enum": [2] }
3038
}
3139
],
3240
"invalid": [
33-
{ "Duration": 2 },
34-
{ "Duration": [2] },
3541
{ "Duration": [4, "score"] }
3642
]
3743
}

examples/Max.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"expression": { "Max": [] },
55
"result": { "enum": [null] }
66
},
7+
{
8+
"expression": { "Max": null },
9+
"result": { "enum": [null] }
10+
},
711
{
812
"expression": { "Max": [3, 2, 1] },
913
"result": { "enum": [3] }
@@ -26,7 +30,6 @@
2630
}
2731
],
2832
"invalid": [
29-
{ "Max": null },
3033
{ "Max": [], "Any": [] }
3134
]
3235
}

examples/Min.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"expression": { "Min": [] },
55
"result": { "enum": [null] }
66
},
7+
{
8+
"expression": { "Min": null },
9+
"result": { "enum": [null] }
10+
},
711
{
812
"expression": { "Min": [3, 2, 1] },
913
"result": { "enum": [1] }
@@ -26,7 +30,6 @@
2630
}
2731
],
2832
"invalid": [
29-
{ "Min": null },
3033
{ "Min": [], "Any": [] }
3134
]
3235
}

examples/Now.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
"expression": { "Now": [] },
55
"result": { "format": "date-time" }
66
},
7+
{
8+
"expression": { "Now": null },
9+
"result": { "format": "date-time" }
10+
},
711
{
812
"expression": { "String": {"Now": []} },
913
"result": { "pattern": "UTC$" }
1014
}
1115
],
1216
"invalid": [
13-
{ "Now": null },
1417
{ "Now": [1] },
1518
{ "Now": 1 }
1619
]

examples/Random.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"expression": { "Random": [] },
55
"result": { "type": "float", "minimum": 0, "maximum": 1 }
66
},
7+
{
8+
"expression": { "Random": null },
9+
"result": { "type": "float", "minimum": 0, "maximum": 1 }
10+
},
711
{
812
"expression": { "Random": 2 },
913
"result": { "type": "integer", "minimum": 0, "maximum": 1 }
@@ -19,7 +23,6 @@
1923
}
2024
],
2125
"invalid": [
22-
{ "Random": null },
2326
{ "Random": [1, 2] }
2427
]
2528
}

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { default as schemas } from '../schemas'
2-
export { default as validator } from './validator'
2+
export { default as validate } from './validate'
33
export { default as examples } from '../examples'

lib/validate.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Ajv from 'ajv'
2+
import addFormats from 'ajv-formats'
3+
import schemas from '../schemas'
4+
5+
const ajv = new Ajv({
6+
schemas: Object.values(schemas),
7+
useDefaults: true,
8+
allErrors: true
9+
})
10+
addFormats(ajv)
11+
const validator = ajv.getSchema(schemas.default.$id)
12+
13+
function coerceArgsToArray(object) {
14+
if (object && typeof object === 'object') {
15+
return Object.fromEntries(Object.entries(object).map(([key, value]) => {
16+
if(value === null) {
17+
value = []
18+
} else if(!Array.isArray(value)){
19+
value = [value]
20+
}
21+
22+
return [key, value.map(coerceArgsToArray)]
23+
}))
24+
} else {
25+
return object
26+
}
27+
}
28+
29+
export default (input) => {
30+
const result = coerceArgsToArray(input)
31+
const valid = validator(result)
32+
const errors = validator.errors
33+
return { valid, errors, result }
34+
}

lib/validator.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)