Update COERCION.md
parent
d1ca4874c0
commit
b430a66d1a
17
COERCION.md
17
COERCION.md
|
@ -9,6 +9,15 @@ Type coercion only happens if there is `type` keyword and if without coercion th
|
||||||
|
|
||||||
If there are multiple types allowed in `type` keyword the coercion will only happen if none of the types match the data and some of the scalar types are present (coercion to/from `object`/`array` is not possible). In this case the validating function will try coercing the data to each type in order until some of them succeeds.
|
If there are multiple types allowed in `type` keyword the coercion will only happen if none of the types match the data and some of the scalar types are present (coercion to/from `object`/`array` is not possible). In this case the validating function will try coercing the data to each type in order until some of them succeeds.
|
||||||
|
|
||||||
|
The table below summarises possible type coercions.
|
||||||
|
|
||||||
|
|from type →<br>to type ↓|string|number|bolean|null|
|
||||||
|
|---|:-:|:-:|:-:|:-:|
|
||||||
|
|string |-|`x`→`""+x`|`false`→`"false"`<br>`true`→`"true"`|`null`→`""`|
|
||||||
|
|number /<br>integer|Valid number /<br>integer: `x`→`+x`<br>|-|`false`→`0`<br>`true`→`1`|`null`→`0`|
|
||||||
|
|boolean |`"false"`→`false`<br>`"true"`→`true`<br>`"abc"`↛<br>`""`↛|`0`→`false`<br>`1`→`true`<br>`x`↛|-|`null`→`false`|
|
||||||
|
|null |`""`→`null`<br>`"null"`↛<br>`"abc"`↛|`0`→`null`<br>`x`↛|`false`→`null`<br>`true`↛|-|
|
||||||
|
|
||||||
|
|
||||||
## Coersion from string values
|
## Coersion from string values
|
||||||
|
|
||||||
|
@ -44,8 +53,8 @@ Always possible, `'' + data` is used
|
||||||
#### To boolean type
|
#### To boolean type
|
||||||
|
|
||||||
Unlike JavaScript, only these numbers can be coerced to `boolean`:
|
Unlike JavaScript, only these numbers can be coerced to `boolean`:
|
||||||
- 1 -> `true`
|
- `1` -> `true`
|
||||||
- 0 -> `false`
|
- `0` -> `false`
|
||||||
|
|
||||||
|
|
||||||
#### To null type
|
#### To null type
|
||||||
|
@ -63,8 +72,8 @@ Unlike JavaScript, only these numbers can be coerced to `boolean`:
|
||||||
|
|
||||||
#### To number/integer types
|
#### To number/integer types
|
||||||
|
|
||||||
- `true` -> 1
|
- `true` -> `1`
|
||||||
- `false` -> 0
|
- `false` -> `0`
|
||||||
|
|
||||||
|
|
||||||
#### To null type
|
#### To null type
|
||||||
|
|
Loading…
Reference in New Issue