Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93584cf
commit ec206e8
Showing
10 changed files
with
322 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,36 @@ | ||
# Coercion | ||
|
||
Structure does type coercion based on the declared [schema](../schema-concept/README.md). It's important to note that it __never__ coerces the following scenarios: | ||
Structure does type coercion based on the declared [schema](../schema-concept/README.md), let's break it into 3 categories: | ||
|
||
- `undefined`; | ||
- `null` when `nullable` option is enabled; | ||
- [Primitive type coercion](primitive-type-coercion.md) | ||
- [Arrays coercion](arrays-and-array-subclasses.md) | ||
- [Generic coercion](generic-coercion.md) | ||
|
||
## Observations | ||
|
||
Structure **never** coerces the following scenarios: | ||
|
||
- value is `undefined`; | ||
- value is `null` when `nullable` option is enabled; | ||
- value is already of the declared type (except for arrays, we'll talk more about this soon). | ||
|
||
Let's break the coercion into 3 categories. | ||
Also, Structure only does **array items coercion** during instantiation, so mutating an array (using push, for example) won't coerce the new item: | ||
|
||
```javascript | ||
const Library = attributes({ | ||
books: { | ||
type: Array, | ||
itemType: String, | ||
}, | ||
})(class Library {}); | ||
|
||
const library = new Library({ | ||
books: [1984], | ||
}); | ||
|
||
library.books; // ['1984'] => coerced number to string | ||
|
||
library.books.push(42); | ||
|
||
library.books; // ['1984', 42] => new item was not coerced | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Disabling coercion | ||
|
||
You can disable coercion for a whole structure or for attributes individually using the `coercion` option in the schema and attribute options, respectively. Notice that it will cause validation to fail when the passed value is not of the expected value: | ||
|
||
## Disabling for the whole structure | ||
|
||
```js | ||
const User = attributes( | ||
{ | ||
name: String, | ||
age: Number, | ||
}, | ||
{ | ||
coercion: false, | ||
} | ||
)(class User {}); | ||
|
||
const user = new User({ name: 123, age: '42' }); | ||
|
||
user.name; // 123 | ||
user.age; // '42' | ||
|
||
const { valid, errors } = user.validate(); | ||
|
||
valid; // false | ||
errors; /* | ||
[ | ||
{ message: '"name" must be a string', path: ['name'] }, | ||
{ message: '"age" must be a number', path: ['age'] } | ||
] | ||
*/ | ||
``` | ||
|
||
## Disabling for specific attributes | ||
|
||
```js | ||
const User = attributes({ | ||
name: { type: String, coercion: false }, | ||
age: Number, | ||
})(class User {}); | ||
|
||
const user = new User({ name: 123, age: '42' }); | ||
|
||
user.name; // 123 | ||
user.age; // 42 | ||
|
||
const { valid, errors } = user.validate(); | ||
|
||
valid; // false | ||
errors; /* | ||
[ | ||
{ message: '"name" must be a string', path: ['name'] } | ||
] | ||
*/ | ||
``` | ||
|
||
## Overwritting structure option with attribute option | ||
|
||
If you define the `coercion` option both for the structure _and_ for an attribute, the structure one will apply for the whole schema except the specific attributes that overwrite it: | ||
|
||
```js | ||
const User = attributes( | ||
{ | ||
name: { type: String, coercion: true }, | ||
age: Number, | ||
}, | ||
{ | ||
coercion: false, | ||
} | ||
)(class User {}); | ||
|
||
const user = new User({ name: 123, age: '42' }); | ||
|
||
user.name; // '123' | ||
user.age; // '42' | ||
|
||
const { valid, errors } = user.validate(); | ||
|
||
valid; // false | ||
errors; /* | ||
[ | ||
{ message: '"age" must be a number', path: ['age'] } | ||
] | ||
*/ | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.