From b430a66d1a6f93617e4f235ecd2bffb8c8f40d00 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sun, 17 Jan 2016 17:30:52 +0000 Subject: [PATCH] Update COERCION.md --- COERCION.md | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/COERCION.md b/COERCION.md index 625505b..eb01c67 100644 --- a/COERCION.md +++ b/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. +The table below summarises possible type coercions. + +|from type →
to type ↓|string|number|bolean|null| +|---|:-:|:-:|:-:|:-:| +|string |-|`x`→`""+x`|`false`→`"false"`
`true`→`"true"`|`null`→`""`| +|number /
integer|Valid number /
integer: `x`→`+x`
|-|`false`→`0`
`true`→`1`|`null`→`0`| +|boolean |`"false"`→`false`
`"true"`→`true`
`"abc"`↛
`""`↛|`0`→`false`
`1`→`true`
`x`↛|-|`null`→`false`| +|null |`""`→`null`
`"null"`↛
`"abc"`↛|`0`→`null`
`x`↛|`false`→`null`
`true`↛|-| + ## Coersion from string values @@ -44,8 +53,8 @@ Always possible, `'' + data` is used #### To boolean type Unlike JavaScript, only these numbers can be coerced to `boolean`: -- 1 -> `true` -- 0 -> `false` +- `1` -> `true` +- `0` -> `false` #### To null type @@ -63,8 +72,8 @@ Unlike JavaScript, only these numbers can be coerced to `boolean`: #### To number/integer types -- `true` -> 1 -- `false` -> 0 +- `true` -> `1` +- `false` -> `0` #### To null type