Skip to content

Commit abf6a34

Browse files
committed
update changelog
1 parent c6f0614 commit abf6a34

File tree

4 files changed

+157
-137
lines changed

4 files changed

+157
-137
lines changed

CHANGELOG.md

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,98 @@
11
--------------------------------------------------
22
<a name="1.0.0"></a>
33
# 1.0.0 (2019-11-xx)
4-
The full module has been rewritten. It uses code generators in order to be much faster.
4+
5+
The full library has been rewritten. It uses code generators in order to be much faster.
56

67
## Breaking changes
8+
This new version contains several breaking changes.
79

810
### Rule logic changed
9-
The rule codes have been rewritten to code generator functions.
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.
1015

1116
## New
1217

1318
### 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`.
1440

1541
### 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+
```
1668

1769
## Other changes
1870

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+
1996
--------------------------------------------------
2097
<a name="0.6.19"></a>
2198
# 0.6.19 (2019-10-25)

README.md

Lines changed: 67 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ v.validate("Al", schema); // Fail, too short.
245245
```
246246

247247
# Sanitizations
248-
The library contains several sanitizations. **Please note, the sanitizers change the original object.**
248+
The library contains several sanitizaters. **Please note, the sanitizers change the original checked object.**
249249

250250
## Default values
251251
The most common sanitizer is the `default` property. With it, you can define a default value for all properties. If the property value is `null` or `undefined`, the validator set the defined default value into the property.
@@ -455,17 +455,29 @@ Property | Default | Description
455455
`values` | `null` | The valid values.
456456

457457
## `equal`
458-
This is an equal value validator.
458+
This is an equal value validator. It checks a value with a static value or with another property.
459459

460+
**Example with static value**:
460461
```js
461462
const schema = {
462-
agreeTerms: { type: "equal", value: true }
463+
agreeTerms: { type: "equal", value: true, strict: true } // strict means `===`
463464
}
464465

465466
v.validate({ agreeTerms: true }, schema); // Valid
466467
v.validate({ agreeTerms: false }, schema); // Fail
467468
```
468469

470+
**Example with other field**:
471+
```js
472+
const schema = {
473+
password: { type: "string", min: 6 },
474+
confirmPassword: { type: "equal", field: "password" }
475+
}
476+
477+
v.validate({ password: "123456", confirmPassword: "123456" }, schema); // Valid
478+
v.validate({ password: "123456", confirmPassword: "pass1234" }, schema); // Fail
479+
```
480+
469481
### Properties
470482
Property | Default | Description
471483
-------- | -------- | -----------
@@ -926,45 +938,58 @@ v.validate({ firstname: "John", lastname: 23 }, schema );
926938
# Message types
927939
Name | Default text
928940
------------------- | -------------
929-
`required` | The '{field}' field is required!
930-
`string` | The '{field}' field must be a string!
931-
`stringEmpty` | The '{field}' field must not be empty!
932-
`stringMin` | The '{field}' field length must be greater than or equal to {expected} characters long!
933-
`stringMax` | The '{field}' field length must be less than or equal to {expected} characters long!
934-
`stringLength` | The '{field}' field length must be {expected} characters long!
935-
`stringPattern` | The '{field}' field fails to match the required pattern!
936-
`stringContains` | The '{field}' field must contain the '{expected}' text!
937-
`stringEnum` | The '{field}' field does not match any of the allowed values!
938-
`number` | The '{field}' field must be a number!
939-
`numberMin` | The '{field}' field must be greater than or equal to {expected}!
940-
`numberMax` | The '{field}' field must be less than or equal to {expected}!
941-
`numberEqual` | The '{field}' field must be equal with {expected}!
942-
`numberNotEqual` | The '{field}' field can't be equal with {expected}!
943-
`numberInteger` | The '{field}' field must be an integer!
944-
`numberPositive` | The '{field}' field must be a positive number!
945-
`numberNegative` | The '{field}' field must be a negative number!
946-
`array` | The '{field}' field must be an array!
947-
`arrayEmpty` | The '{field}' field must not be an empty array!
948-
`arrayMin` | The '{field}' field must contain at least {expected} items!
949-
`arrayMax` | The '{field}' field must contain less than or equal to {expected} items!
950-
`arrayLength` | The '{field}' field must contain {expected} items!
951-
`arrayContains` | The '{field}' field must contain the '{expected}' item!
952-
`arrayEnum` | The '{field} field value '{expected}' does not match any of the allowed values!
953-
`boolean` | The '{field}' field must be a boolean!
954-
`function` | The '{field}' field must be a function!
955-
`date` | The '{field}' field must be a Date!
956-
`dateMin` | The '{field}' field must be greater than or equal to {expected}!
957-
`dateMax` | The '{field}' field must be less than or equal to {expected}!
958-
`forbidden` | The '{field}' field is forbidden!
959-
`email` | The '{field}' field must be a valid e-mail!
941+
`required` | The '{field}' field is required.
942+
`string` | The '{field}' field must be a string.
943+
`stringEmpty` | The '{field}' field must not be empty.
944+
`stringMin` | The '{field}' field length must be greater than or equal to {expected} characters long.
945+
`stringMax` | The '{field}' field length must be less than or equal to {expected} characters long.
946+
`stringLength` | The '{field}' field length must be {expected} characters long.
947+
`stringPattern` | The '{field}' field fails to match the required pattern.
948+
`stringContains` | The '{field}' field must contain the '{expected}' text.
949+
`stringEnum` | The '{field}' field does not match any of the allowed values.
950+
`stringNumeric` | The '{field}' field must be a numeric string.
951+
`stringAlpha` | The '{field}' field must be an alphabetic string.
952+
`stringAlphanum` | The '{field}' field must be an alphanumeric string.
953+
`stringAlphadash` | The '{field}' field must be an alphadash string.
954+
`number` | The '{field}' field must be a number.
955+
`numberMin` | The '{field}' field must be greater than or equal to {expected}.
956+
`numberMax` | The '{field}' field must be less than or equal to {expected}.
957+
`numberEqual` | The '{field}' field must be equal to {expected}.
958+
`numberNotEqual` | The '{field}' field can't be equal to {expected}.
959+
`numberInteger` | The '{field}' field must be an integer.
960+
`numberPositive` | The '{field}' field must be a positive number.
961+
`numberNegative` | The '{field}' field must be a negative number.
962+
`array` | The '{field}' field must be an array.
963+
`arrayEmpty` | The '{field}' field must not be an empty array.
964+
`arrayMin` | The '{field}' field must contain at least {expected} items.
965+
`arrayMax` | The '{field}' field must contain less than or equal to {expected} items.
966+
`arrayLength` | The '{field}' field must contain {expected} items.
967+
`arrayContains` | The '{field}' field must contain the '{expected}' item.
968+
`arrayEnum` | The '{actual}' value in '{field}' field does not match any of the '{expected}' values.
969+
`boolean` | The '{field}' field must be a boolean.
970+
`function` | The '{field}' field must be a function.
971+
`date` | The '{field}' field must be a Date.
972+
`dateMin` | The '{field}' field must be greater than or equal to {expected}.
973+
`dateMax` | The '{field}' field must be less than or equal to {expected}.
974+
`forbidden` | The '{field}' field is forbidden.
975+
`email` | The '{field}' field must be a valid e-mail.
976+
`url` | The '{field}' field must be a valid URL.
977+
`enumValue` | The '{field}' field value '{expected}' does not match any of the allowed values.
978+
`equalValue` | The '{field}' field value must be equal to '{expected}'.
979+
`equalField` | The '{field}' field value must be equal to '{expected}' field value.
980+
`object` | The '{field}' must be an Object.
981+
`objectStrict` | The object '{field}' contains forbidden keys: '{actual}'.
982+
`uuid` | The '{field}' field must be a valid UUID.
983+
`uuidVersion` | The '{field}' field must be a valid UUID version provided.
984+
`mac` | The '{field}' field must be a valid MAC address.
985+
`luhn` | The '{field}' field must be a valid checksum luhn.
960986

961987
## Message fields
962988
Name | Description
963989
----------- | -------------
964990
`field` | The field name
965991
`expected` | The expected value
966992
`actual` | The actual value
967-
`type` | The field type
968993

969994
# Development
970995
```
@@ -981,28 +1006,30 @@ npm test
9811006
-----------------|----------|----------|----------|----------|-------------------|
9821007
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
9831008
-----------------|----------|----------|----------|----------|-------------------|
984-
All files | 100 | 100 | 100 | 100 | |
1009+
All files | 100 | 97.73 | 100 | 100 | |
9851010
lib | 100 | 100 | 100 | 100 | |
9861011
messages.js | 100 | 100 | 100 | 100 | |
9871012
validator.js | 100 | 100 | 100 | 100 | |
9881013
lib/helpers | 100 | 100 | 100 | 100 | |
9891014
deep-extend.js | 100 | 100 | 100 | 100 | |
9901015
flatten.js | 100 | 100 | 100 | 100 | |
991-
lib/rules | 100 | 100 | 100 | 100 | |
1016+
lib/rules | 100 | 96.43 | 100 | 100 | |
9921017
any.js | 100 | 100 | 100 | 100 | |
9931018
array.js | 100 | 100 | 100 | 100 | |
9941019
boolean.js | 100 | 100 | 100 | 100 | |
995-
custom.js | 100 | 100 | 100 | 100 | |
1020+
custom.js | 100 | 50 | 100 | 100 | 6 |
9961021
date.js | 100 | 100 | 100 | 100 | |
9971022
email.js | 100 | 100 | 100 | 100 | |
998-
enum.js | 100 | 100 | 100 | 100 | |
1023+
enum.js | 100 | 50 | 100 | 100 | 6 |
1024+
equal.js | 100 | 100 | 100 | 100 | |
9991025
forbidden.js | 100 | 100 | 100 | 100 | |
10001026
function.js | 100 | 100 | 100 | 100 | |
10011027
luhn.js | 100 | 100 | 100 | 100 | |
10021028
mac.js | 100 | 100 | 100 | 100 | |
1029+
multi.js | 100 | 100 | 100 | 100 | |
10031030
number.js | 100 | 100 | 100 | 100 | |
10041031
object.js | 100 | 100 | 100 | 100 | |
1005-
string.js | 100 | 100 | 100 | 100 | |
1032+
string.js | 100 | 95.83 | 100 | 100 | 55,63 |
10061033
url.js | 100 | 100 | 100 | 100 | |
10071034
uuid.js | 100 | 100 | 100 | 100 | |
10081035
-----------------|----------|----------|----------|----------|-------------------|

TODO.md

Lines changed: 0 additions & 95 deletions
This file was deleted.

test/validator.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ describe("Test getRuleFromSchema method", () => {
188188
});
189189
});
190190

191+
describe("Test makeError", () => {
192+
193+
const v = new Validator();
194+
195+
it("should generate an error creation code", () => {
196+
expect(v.makeError({ type: "required", messages: v.messages })).toBe("errors.push({ type: \"required\", message: \"The '{field}' field is required.\", field: field });");
197+
expect(v.makeError({ type: "stringMin", field: "firstName", expected: 6, actual: 3, messages: v.messages })).toBe("errors.push({ type: \"stringMin\", message: \"The '{field}' field length must be greater than or equal to {expected} characters long.\", field: \"firstName\", expected: 6, actual: 3 });");
198+
});
199+
200+
});
201+
191202
describe("Test compile (integration test)", () => {
192203

193204

0 commit comments

Comments
 (0)