fixed bug: custom sync keyword in async schema

master
Evgeny Poberezkin 2016-02-04 19:56:04 +00:00
parent 38827f50d4
commit 5db986276c
8 changed files with 285 additions and 5 deletions

View File

@ -8,11 +8,11 @@
, $i = 'i' + $lvl
, $ruleErr = 'ruleErr' + $lvl
, $rDef = $rule.definition
, $async = $rDef.async
, $asyncKeyword = $rDef.async
, $inline = $rDef.inline
, $macro = $rDef.macro;
if ($async && !it.async)
if ($asyncKeyword && !it.async)
throw new Error('async keyword in sync schema');
}}
@ -29,7 +29,7 @@ var {{=$errs}} = errors;
{{?? $macro }}
valid{{=$it.level}}
{{??}}
{{?$async}}{{=it.yieldAwait}} {{?}}{{=$ruleValidate.code}}.call(self
{{?$asyncKeyword}}{{=it.yieldAwait}} {{?}}{{=$ruleValidate.code}}.call(self
{{? $rDef.compile }}
, {{=$data}}
{{??}}

View File

@ -58,7 +58,7 @@
"istanbul": "^0.4.2",
"js-beautify": "^1.5.6",
"jshint": "^2.8.0",
"json-schema-test": "^1.1.0",
"json-schema-test": "^1.1.1",
"karma": "^0.13.3",
"karma-chrome-launcher": "^0.2.0",
"karma-mocha": "^0.2.0",

View File

@ -25,7 +25,7 @@ function getAjvInstances(opts) {
{ async: 'co*', allErrors: true },
{ async: 'es7', allErrors: true },
{ async: 'es7', transpile: 'nodent', allErrors: true },
{ async: '*', transpile: 'regenerator', allErrors: true },
{ async: '*', transpile: 'regenerator', allErrors: true }
];
if (fullTest) options = options.concat([

134
spec/async/compound.json Normal file
View File

@ -0,0 +1,134 @@
[
{
"description": "allOf: async + sync",
"schema": {
"$async": true,
"allOf": [
{
"idExists": { "table": "users" }
},
{
"type": "integer",
"minimum": 3
}
]
},
"tests": [
{
"description": "valid id",
"data": 5,
"valid": true
},
{
"description": "another valid id",
"data": 8,
"valid": true
},
{
"description": "invalid async - not user id",
"data": 9,
"valid": false
},
{
"description": "invalid sync - valid id but too small",
"data": 1,
"valid": false
}
]
},
{
"description": "anyOf: async + sync",
"schema": {
"$async": true,
"anyOf": [
{
"idExists": { "table": "users" }
},
{
"type": "integer",
"minimum": 3
}
]
},
"tests": [
{
"description": "valid id",
"data": 1,
"valid": true
},
{
"description": "valid - not id but big enough",
"data": 4,
"valid": true
},
{
"description": "valid - id and big enough",
"data": 5,
"valid": true
},
{
"description": "invalid both",
"data": 2,
"valid": false
}
]
},
{
"description": "oneOf: async + sync",
"schema": {
"$async": true,
"oneOf": [
{
"idExists": { "table": "users" }
},
{
"type": "integer",
"minimum": 3
}
]
},
"tests": [
{
"description": "valid id",
"data": 1,
"valid": true
},
{
"description": "valid - not id but big enough",
"data": 4,
"valid": true
},
{
"description": "invalid - id and big enough",
"data": 5,
"valid": false
},
{
"description": "invalid both",
"data": 2,
"valid": false
}
]
},
{
"description": "not with async",
"schema": {
"$async": true,
"not": {
"idExists": { "table": "users" }
}
},
"tests": [
{
"description": "invalid because valid id",
"data": 1,
"valid": false
},
{
"description": "valid because not a valid id",
"data": 4,
"valid": true
}
]
}
]

48
spec/async/items.json Normal file
View File

@ -0,0 +1,48 @@
[
{
"description": "items: async + sync",
"schema": {
"$async": true,
"items": [
{
"type": "integer",
"idExists": { "table": "users" }
},
{
"type": "integer"
},
{
"type": "integer",
"idExists": { "table": "users" }
}
]
},
"tests": [
{
"description": "valid array",
"data": [ 1, 2, 5 ],
"valid": true
},
{
"description": "another valid array",
"data": [ 5, 2, 8 ],
"valid": true
},
{
"description": "invalid 1st async item",
"data": [ 9, 2, 8 ],
"valid": false
},
{
"description": "invalid 2nd async item",
"data": [ 1, 2, 9 ],
"valid": false
},
{
"description": "invalid sync item",
"data": [ 1, "abc", 5 ],
"valid": false
}
]
}
]

View File

@ -84,5 +84,24 @@
"valid": false
}
]
},
{
"description": "custom keyword in async schema",
"schema": {
"$async": true,
"constant": 5
},
"tests": [
{
"description": "valid",
"data": 5,
"valid": true
},
{
"description": "valid",
"data": 1,
"valid": false
}
]
}
]

View File

@ -0,0 +1,39 @@
[
{
"description": "properties: async + sync",
"schema": {
"$async": true,
"properties": {
"foo": {
"type": "integer",
"idExists": { "table": "users" }
},
"bar": {
"type": "integer"
}
}
},
"tests": [
{
"description": "valid object",
"data": { "foo": 1, "bar": 2 },
"valid": true
},
{
"description": "another valid object",
"data": { "foo": 5, "bar": 2 },
"valid": true
},
{
"description": "invalid sync property",
"data": { "foo": 1, "bar": "abc" },
"valid": false
},
{
"description": "invalid async property",
"data": { "foo": 9, "bar": 2 },
"valid": false
}
]
}
]

40
spec/async/switch.json Normal file
View File

@ -0,0 +1,40 @@
[
{
"description": "switch: async + sync conditions",
"schema": {
"$async": true,
"switch": [
{
"if": { "idExists": { "table": "users" } },
"then": { "minimum": 6 }
},
{
"if": { "minimum": 21 },
"then": { "idExists": { "table": "posts" } }
}
]
},
"tests": [
{
"description": "valid - first condition",
"data": 8,
"valid": true
},
{
"description": "valid - second condition",
"data": 28,
"valid": true
},
{
"description": "invalid - first condition",
"data": 1,
"valid": false
},
{
"description": "invalid - second condition",
"data": 22,
"valid": false
}
]
}
]