|
| 1 | +-------------------------------------------------- |
| 2 | +<a name="1.0.0"></a> |
| 3 | +# 1.0.0 (2019-11-xx) |
| 4 | + |
| 5 | +The full library has been rewritten. It uses code generators in order to be much faster. |
| 6 | + |
| 7 | +## Breaking changes |
| 8 | +This new version contains several breaking changes. |
| 9 | + |
| 10 | +### Rule logic changed |
| 11 | +The rule codes have been rewritten to code generator functions. Therefore if you use custom validators, you should rewrite them after upgrading. |
| 12 | + |
| 13 | +### Convert values |
| 14 | +The `number`, `boolean` and `date` rules have a `convert: true` property. In the previous version it doesn't modify the value in the checked object, just converted the value to the rules. In the version 1.0 this property converts the values in the checked object, as well. |
| 15 | + |
| 16 | +## New |
| 17 | + |
| 18 | +### Sanitizations |
| 19 | +The sanitization function is implemented. There are several rules which contains sanitizers. **Please note, the sanitizers change the original checked object values.** |
| 20 | + |
| 21 | +| Rule | Property | Description | |
| 22 | +| ---- | -------- | ----------- | |
| 23 | +`boolean` | `convert` | Convert the value to a boolean. |
| 24 | +`number` | `convert` | Convert the value to a number. |
| 25 | +`date` | `convert` | Convert the value to a date. |
| 26 | +`string` | `trim` | Trim the value. |
| 27 | +`string` | `trimLeft` | Left trim the value. |
| 28 | +`string` | `trimRight` | Right trim the value. |
| 29 | +`string` | `lowercase` | Lowercase the value. |
| 30 | +`string` | `uppercase` | Uppercase the value. |
| 31 | +`string` | `localeLowercase` | Lowercase the value with `String.toLocaleLowerCase`. |
| 32 | +`string` | `localeUppercase` | Uppercase the value with `String.toLocaleUpperCase`. |
| 33 | +`string` | `padStart` | Left padding the value. |
| 34 | +`string` | `padEnd` | Right padding the value. |
| 35 | +`string` | `convert` | Convert the value to a string. |
| 36 | +`email` | `normalize` | Trim & lowercase the value. |
| 37 | +`forbidden` | `remove` | Remove the forbidden field. |
| 38 | +`object` | `strict: "remove"` | Remove additional properties in the object. |
| 39 | +`*` | `default` | Use this default value if the value is `null` or `undefined`. |
| 40 | + |
| 41 | +### Root element validation |
| 42 | +Basically the validator expects that you want to validate a Javascript object. If you want others, you can define the root level schema, as well. In this case set the `$$root: true` property. |
| 43 | + |
| 44 | +**Example to validate a `string` variable instead of `object`** |
| 45 | +```js |
| 46 | +const schema = { |
| 47 | + $$root: true, |
| 48 | + type: "string", |
| 49 | + min: 3, |
| 50 | + max: 6 |
| 51 | +}; |
| 52 | + |
| 53 | +v.validate("John", schema); // Valid |
| 54 | +v.validate("Al", schema); // Fail, too short. |
| 55 | +``` |
| 56 | + |
| 57 | +### Enhanced shorthand types |
| 58 | +You can use string-based shorthand validation definitions in the schema with properties. |
| 59 | + |
| 60 | +```js |
| 61 | +{ |
| 62 | + password: "string|min:6", |
| 63 | + age: "number|optional|integer|positive|min:0|max:99", |
| 64 | + |
| 65 | + retry: ["number|integer|min:0", "boolean"] // multiple types |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +## Other changes |
| 70 | + |
| 71 | +### New `equal` rule |
| 72 | +It checks the value equal (`==`) to a static value or another property. The `strict` property uses `===` to check values. |
| 73 | + |
| 74 | +**Example with static value**: |
| 75 | +```js |
| 76 | +const schema = { |
| 77 | + agreeTerms: { type: "equal", value: true, strict: true } // strict means `===` |
| 78 | +} |
| 79 | + |
| 80 | +v.validate({ agreeTerms: true }, schema); // Valid |
| 81 | +v.validate({ agreeTerms: false }, schema); // Fail |
| 82 | +``` |
| 83 | + |
| 84 | +**Example with other field**: |
| 85 | +```js |
| 86 | +const schema = { |
| 87 | + password: { type: "string", min: 6 }, |
| 88 | + confirmPassword: { type: "equal", field: "password" } |
| 89 | +} |
| 90 | + |
| 91 | +v.validate({ password: "123456", confirmPassword: "123456" }, schema); // Valid |
| 92 | +v.validate({ password: "123456", confirmPassword: "pass1234" }, schema); // Fail |
| 93 | +``` |
| 94 | + |
| 95 | +### `properties` in object rule |
| 96 | +You can use the `properties` property besides the `props` property in the object rule. |
| 97 | + |
1 | 98 | -------------------------------------------------- |
2 | 99 | <a name="0.6.19"></a> |
3 | 100 | # 0.6.19 (2019-10-25) |
|
0 commit comments