valleyed 4.1.4 → 4.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -0
- package/README.md +6 -6
- package/lib/validators/index.d.ts +2 -4
- package/lib/validators/index.js +24 -18
- package/lib/validators/index.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [4.1.5](https://github.com/kevinand11/valleyed/compare/v4.1.4...v4.1.5) (2023-03-19)
|
|
6
|
+
|
|
5
7
|
### [4.1.4](https://github.com/kevinand11/valleyed/compare/v4.1.3...v4.1.4) (2023-03-18)
|
|
6
8
|
|
|
7
9
|
|
package/README.md
CHANGED
|
@@ -221,24 +221,24 @@ const res = isCustom((value) => typeof value === 'function')(() => {}) // res.va
|
|
|
221
221
|
import { Validator, isEmail, isMinOf, isString, isNumber } from 'valleyed'
|
|
222
222
|
|
|
223
223
|
// The Validator.and function is used to build up a schema or list of rules to validate a value against
|
|
224
|
-
let res = Validator.and(
|
|
224
|
+
let res = Validator.and([[isEmail(), isMinOf(1)]])('johndoe@mail.com')
|
|
225
225
|
console.log(res) // { valid: true, errors: [], value: 'johndoe@mail.com' }
|
|
226
226
|
|
|
227
227
|
// if the value fails validation, it returns a list of all errors in the errors array
|
|
228
|
-
res = Validator.and(
|
|
228
|
+
res = Validator.and([[isEmail(), isMinOf(1)]])('')
|
|
229
229
|
console.log(res) // { valid: false, value: '', errors: [ 'is not a valid email', 'must contain 1 or more characters' ] }
|
|
230
230
|
|
|
231
231
|
// Similar to the And function, Validator has an or function that checks if the value passes validation for any of the list of rules passed in
|
|
232
|
-
let res = Validator.or(
|
|
232
|
+
let res = Validator.or([[isString(), isMinOf(1)], [isNumber()]])(2)
|
|
233
233
|
console.log(res) // { valid: true, value: 2, errors: [] }
|
|
234
234
|
|
|
235
235
|
// if the value fails validation, it returns a list of all errors in the errors array
|
|
236
|
-
res = Validator.or(
|
|
236
|
+
res = Validator.or([[isString(), isMinOf(1)], [isNumber()]])(false)
|
|
237
237
|
console.log(res) // { valid: false, value: false, errors: [ 'doesn't match any of the schema' ] }
|
|
238
238
|
|
|
239
239
|
// An optional third paramater can be passed into the And/Or functions to control if null and undefined are allowed to pass validation
|
|
240
|
-
Validator.and(
|
|
240
|
+
Validator.and([[isEmail()]], {
|
|
241
241
|
nullable: true, // Boolean: if true, null passed as the first argument passes validation
|
|
242
242
|
required: () => false // Boolean or Function that returns a boolean: if false, undefined passed as the first argument passes validation
|
|
243
|
-
})
|
|
243
|
+
})('')
|
|
244
244
|
```
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { Options, Rule } from '../utils/rules';
|
|
2
|
-
type Validity<T> = ReturnType<Rule<T>>;
|
|
3
2
|
export declare class Validator {
|
|
4
|
-
static and<T>(
|
|
5
|
-
static or<T>(
|
|
3
|
+
static and<T>(rules: Rule<T>[][], options?: Partial<Options>): Rule<T>;
|
|
4
|
+
static or<T>(rules: Rule<T>[][], options?: Partial<Options>): Rule<T>;
|
|
6
5
|
}
|
|
7
|
-
export {};
|
package/lib/validators/index.js
CHANGED
|
@@ -3,25 +3,31 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Validator = void 0;
|
|
4
4
|
const rules_1 = require("../utils/rules");
|
|
5
5
|
class Validator {
|
|
6
|
-
static and(
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
6
|
+
static and(rules, options) {
|
|
7
|
+
return (0, rules_1.makeRule)((val) => {
|
|
8
|
+
const value = val;
|
|
9
|
+
for (const rule of rules) {
|
|
10
|
+
const valid = (0, rules_1.check)(value, rule, options);
|
|
11
|
+
if (valid.valid)
|
|
12
|
+
continue;
|
|
13
|
+
else
|
|
14
|
+
return valid;
|
|
15
|
+
}
|
|
16
|
+
return { valid: true, value: value, errors: [] };
|
|
17
|
+
});
|
|
15
18
|
}
|
|
16
|
-
static or(
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
static or(rules, options) {
|
|
20
|
+
return (0, rules_1.makeRule)((val) => {
|
|
21
|
+
const value = val;
|
|
22
|
+
for (const rule of rules) {
|
|
23
|
+
const valid = (0, rules_1.check)(value, rule, options);
|
|
24
|
+
if (valid.valid)
|
|
25
|
+
return valid;
|
|
26
|
+
else
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
return { valid: false, value: value, errors: ['doesn\'t match any of the schema'] };
|
|
30
|
+
});
|
|
25
31
|
}
|
|
26
32
|
}
|
|
27
33
|
exports.Validator = Validator;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/validators/index.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/validators/index.ts"],"names":[],"mappings":";;;AAAA,0CAA+D;AAE/D,MAAa,SAAS;IACd,MAAM,CAAC,GAAG,CAAK,KAAkB,EAAE,OAA0B;QACnE,OAAO,IAAA,gBAAQ,EAAI,CAAC,GAAG,EAAE,EAAE;YAC1B,MAAM,KAAK,GAAG,GAAQ,CAAA;YACtB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;gBACzB,MAAM,KAAK,GAAG,IAAA,aAAK,EAAC,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA;gBACzC,IAAI,KAAK,CAAC,KAAK;oBAAE,SAAQ;;oBACpB,OAAO,KAAK,CAAA;aACjB;YACD,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,CAAA;QACjD,CAAC,CAAC,CAAA;IACH,CAAC;IAGM,MAAM,CAAC,EAAE,CAAK,KAAkB,EAAE,OAA0B;QAClE,OAAO,IAAA,gBAAQ,EAAI,CAAC,GAAG,EAAE,EAAE;YAC1B,MAAM,KAAK,GAAG,GAAQ,CAAA;YACtB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;gBACzB,MAAM,KAAK,GAAG,IAAA,aAAK,EAAC,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA;gBACzC,IAAI,KAAK,CAAC,KAAK;oBAAE,OAAO,KAAK,CAAA;;oBACxB,SAAQ;aACb;YACD,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,kCAAkC,CAAC,EAAE,CAAA;QACpF,CAAC,CAAC,CAAA;IACH,CAAC;CACD;AAzBD,8BAyBC"}
|
package/package.json
CHANGED