Skip to content

Commit

Permalink
fix: select should support number, boolean and null (fix #150)
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Mar 24, 2021
1 parent 0fe9292 commit 91ad853
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
53 changes: 53 additions & 0 deletions spec/tests/select.json
Original file line number Diff line number Diff line change
Expand Up @@ -470,5 +470,58 @@
"valid": false
}
]
},
{
"description": "select keyword with boolean selector tag (issue #150)",
"schema": {
"type": "object",
"properties": {
"tag": {"type": "boolean"}
},
"select": {
"$data": "0/tag"
},
"selectCases": {
"false": {
"not": {"required": ["foo"]}
},
"true": {
"required": ["foo"]
}
},
"selectDefault": false
},
"tests": [
{
"description": "false tag: valid",
"data": {"tag": false},
"valid": true
},
{
"description": "false tag: invalid",
"data": {"tag": false, "foo": 1},
"valid": false
},
{
"description": "true tag: valid",
"data": {"tag": true, "foo": 1},
"valid": true
},
{
"description": "true tag: invalid",
"data": {"tag": true},
"valid": false
},
{
"description": "no tag - select not validated 1 (valid)",
"data": {"foo": 1},
"valid": true
},
{
"description": "no tag - select not validated 1 (valid)",
"data": {},
"valid": true
}
]
}
]
2 changes: 1 addition & 1 deletion src/definitions/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function getDef(opts?: DefinitionOptions): KeywordDefinition[] {
gen.if(false) // optimizer should remove it from generated code
for (const schemaProp in parentSchema.selectCases) {
cxt.setParams({schemaProp})
gen.elseIf(_`${value} == ${schemaProp}`) // intentional ==, to match numbers and booleans
gen.elseIf(_`"" + ${value} == ${schemaProp}`) // intentional ==, to match numbers and booleans
const schCxt = cxt.subschema({keyword: "selectCases", schemaProp}, schValid)
cxt.mergeEvaluated(schCxt, Name)
gen.assign(valid, schValid)
Expand Down

0 comments on commit 91ad853

Please sign in to comment.