From 19c8a967b89cbfd7ae5e819c58993ccdea2012b6 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Thu, 9 Nov 2017 20:34:00 +0000 Subject: [PATCH] docs: add if/then/else, remove switch --- KEYWORDS.md | 72 ++++++++++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 45 deletions(-) diff --git a/KEYWORDS.md b/KEYWORDS.md index c53e15e..b855416 100644 --- a/KEYWORDS.md +++ b/KEYWORDS.md @@ -10,7 +10,7 @@ The keywords and their values define what rules the data should satisfy to be va - [type](#type) - [Keywords for numbers](#keywords-for-numbers) - - [maximum / minimum and exclusiveMaximum / exclusiveMinimum](#maximum--minimum-and-exclusivemaximum--exclusiveminimum) (CHANGED in draft 6) + - [maximum / minimum and exclusiveMaximum / exclusiveMinimum](#maximum--minimum-and-exclusivemaximum--exclusiveminimum) (CHANGED in draft-06) - [multipleOf](#multipleof) - [Keywords for strings](#keywords-for-strings) - [maxLength/minLength](#maxlength--minlength) @@ -22,7 +22,7 @@ The keywords and their values define what rules the data should satisfy to be va - [uniqueItems](#uniqueitems) - [items](#items) - [additionalItems](#additionalitems) - - [contains](#contains) (NEW in draft 6) + - [contains](#contains) (added in draft-06) - [Keywords for objects](#keywords-for-objects) - [maxProperties/minProperties](#maxproperties--minproperties) - [required](#required) @@ -30,18 +30,18 @@ The keywords and their values define what rules the data should satisfy to be va - [patternProperties](#patternproperties) - [additionalProperties](#additionalproperties) - [dependencies](#dependencies) - - [propertyNames](#propertynames) (NEW in draft 6) + - [propertyNames](#propertynames) (added in draft-06) - [patternGroups](#patterngroups-deprecated) (deprecated) - [patternRequired](#patternrequired-proposed) (proposed) - [Keywords for all types](#keywords-for-all-types) - [enum](#enum) - - [const](#const) (NEW in draft 6) + - [const](#const) (added in draft-06) - [Compound keywords](#compound-keywords) - [not](#not) - [oneOf](#oneof) - [anyOf](#anyof) - [allOf](#allof) - - [switch](#switch-proposed) (proposed) + - [if/then/else](#ifthenelse) (NEW in draft-07) @@ -830,29 +830,15 @@ _invalid_: `1.5`, `2.5`, `4`, `4.5`, `5`, `5.5`, any non-number -### `switch` (proposed) +### `if`/`then`/`else` -Defined in [ajv-keywords](https://github.com/epoberezkin/ajv-keywords) package. +These keywords allow to implement conditional validation. Their values should be valid JSON-schemas (object or boolean). -The value of the keyword is the array of if/then clauses. Each clause is the object with the following properties: +If `if` keyword is absent, the validation succeds. -- `if` (optional) - the value is JSON-schema -- `then` (required) - the value is JSON-schema or boolean -- `continue` (optional) - the value is boolean +If the data is valid against the sub-schema in `if` keyword, then the validation result is equal to the result of data validation against the sub-schema in `then` keyword (if `then` is absent, the validation succeeds). -The validation process is dynamic; all clauses are executed sequentially in the following way: - -1. `if`: - 1. `if` property is JSON-schema according to which the data is: - 1. valid => go to step 2. - 2. invalid => go to the NEXT clause, if this was the last clause the validation of `switch` SUCCEEDS. - 2. `if` property is absent => go to step 2. -2. `then`: - 1. `then` property is `true` or it is JSON-schema according to which the data is valid => go to step 3. - 2. `then` property is `false` or it is JSON-schema according to which the data is invalid => the validation of `switch` FAILS. -3. `continue`: - 1. `continue` property is `true` => go to the NEXT clause, if this was the last clause the validation of `switch` SUCCEEDS. - 2. `continue` property is `false` or absent => validation of `switch` SUCCEEDS. +If the data is invalid against the sub-schema in `if` keyword, then the validation result is equal to the result of data validation against the sub-schema in `else` keyword (if `else` is absent, the validation succeeds). __Examples__ @@ -861,29 +847,24 @@ __Examples__ ```json { - "switch": [ - { - "if": { "properties": { "power": { "minimum": 9000 } } }, - "then": { "required": [ "disbelief" ] }, - "continue": true - }, - { "then": { "required": [ "confidence" ] } } - ] + "if": { "properties": { "power": { "minimum": 9000 } } }, + "then": { "required": [ "disbelief" ] }, + "else": { "required": [ "confidence" ] } } ``` _valid_: - - `{ "power": 9000, "disbelief": true, "confidence": true }` - - `{ "confidence": true }` + - `{ "power": 10000, "disbelief": true }` + - `{}` - `{ "power": 1000, "confidence": true }` + - any non-object _invalid_: - - `{ "power": 9000 }` (`disbelief` & `confidence` are required) - - `{ "power": 9000, "disbelief": true }` (`confidence` is always required) - - `{ "power": 1000 }` - - `{}` + - `{ "power": 10000 }` (`disbelief` is required) + - `{ "power": 10000, "confidence": true }` (`disbelief` is required) + - `{ "power": 1000 }` (`confidence` is required) 2. _schema_: @@ -891,13 +872,14 @@ __Examples__ ```json { "type": "integer", - "switch": [ - { "if": { "not": { "minimum": 1 } }, "then": false }, - { "if": { "maximum": 10 }, "then": true }, - { "if": { "maximum": 100 }, "then": { "multipleOf": 10 } }, - { "if": { "maximum": 1000 }, "then": { "multipleOf": 100 } }, - { "then": false } - ] + "minimum": 1, + "maximum": 1000, + "if": { "minimum": 100 }, + "then": { "multipleOf": 100 }, + "else": { + "if": { "minimum": 10 }, + "then": { "multipleOf": 10 } + } } ```