valgen 6.0.3 → 6.2.0

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.
Files changed (81) hide show
  1. package/constants.js +1 -1
  2. package/core/validation-error.js +1 -0
  3. package/index.d.ts +66 -63
  4. package/index.js +12 -1
  5. package/package.json +3 -2
  6. package/rules/format-rules/is-alpha.js +1 -1
  7. package/rules/format-rules/is-alphanumeric.js +1 -1
  8. package/rules/format-rules/is-ascii.js +1 -1
  9. package/rules/format-rules/is-base64.js +2 -2
  10. package/rules/format-rules/is-btc-address.js +1 -1
  11. package/rules/format-rules/is-credit-card.js +2 -2
  12. package/rules/format-rules/is-decimal.js +1 -1
  13. package/rules/format-rules/is-ean.js +1 -1
  14. package/rules/format-rules/is-email.js +2 -2
  15. package/rules/format-rules/is-eth-address.js +1 -1
  16. package/rules/format-rules/is-fqdn.js +2 -2
  17. package/rules/format-rules/is-hash.js +2 -2
  18. package/rules/format-rules/is-hex-color.js +1 -1
  19. package/rules/format-rules/is-hex.js +1 -1
  20. package/rules/format-rules/is-iban.js +1 -1
  21. package/rules/format-rules/is-ip.js +4 -3
  22. package/rules/format-rules/is-issn.js +2 -2
  23. package/rules/format-rules/is-jwt.js +1 -1
  24. package/rules/format-rules/is-lowercase.js +1 -1
  25. package/rules/format-rules/is-mac-address.js +3 -2
  26. package/rules/format-rules/is-mobile-phone.js +2 -2
  27. package/rules/format-rules/is-object-id.js +1 -1
  28. package/rules/format-rules/is-passport-number.js +1 -1
  29. package/rules/format-rules/is-port.js +2 -1
  30. package/rules/format-rules/is-swift.js +1 -1
  31. package/rules/format-rules/is-time.js +1 -1
  32. package/rules/format-rules/is-uppercase.js +1 -1
  33. package/rules/format-rules/is-url.js +2 -2
  34. package/rules/format-rules/is-uuid.js +2 -2
  35. package/rules/format-rules/is-vat-number.js +2 -2
  36. package/rules/format-rules/matches.js +1 -1
  37. package/rules/index.d.ts +3 -0
  38. package/rules/index.js +3 -0
  39. package/rules/logical-rules/is-defined.js +1 -1
  40. package/rules/logical-rules/is-empty.js +1 -1
  41. package/rules/logical-rules/is-equal.js +2 -2
  42. package/rules/logical-rules/is-gt.js +1 -1
  43. package/rules/logical-rules/is-gte.d.ts +1 -1
  44. package/rules/logical-rules/is-gte.js +1 -1
  45. package/rules/logical-rules/is-lt.js +1 -1
  46. package/rules/logical-rules/is-lte.js +1 -1
  47. package/rules/logical-rules/range.js +1 -1
  48. package/rules/type-rules/is-any.js +1 -1
  49. package/rules/type-rules/is-array.d.ts +1 -1
  50. package/rules/type-rules/is-array.js +2 -2
  51. package/rules/type-rules/is-bigint.js +1 -1
  52. package/rules/type-rules/is-boolean.js +1 -1
  53. package/rules/type-rules/is-date.js +3 -2
  54. package/rules/type-rules/is-enum.js +1 -1
  55. package/rules/type-rules/is-instanceof.d.ts +11 -0
  56. package/rules/type-rules/is-instanceof.js +17 -0
  57. package/rules/type-rules/is-integer.js +1 -1
  58. package/rules/type-rules/is-null.js +4 -4
  59. package/rules/type-rules/is-number.js +1 -1
  60. package/rules/type-rules/is-object.js +1 -1
  61. package/rules/type-rules/is-record.d.ts +1 -1
  62. package/rules/type-rules/is-record.js +2 -2
  63. package/rules/type-rules/is-string.js +1 -1
  64. package/rules/type-rules/is-tuple.js +1 -1
  65. package/rules/type-rules/is-undefined.js +1 -1
  66. package/rules/utility-rules/all-of.d.ts +1 -1
  67. package/rules/utility-rules/all-of.js +2 -2
  68. package/rules/utility-rules/exists.js +1 -1
  69. package/rules/utility-rules/fixed.d.ts +5 -0
  70. package/rules/utility-rules/fixed.js +10 -0
  71. package/rules/utility-rules/get-length.js +1 -1
  72. package/rules/utility-rules/nullable.d.ts +11 -0
  73. package/rules/utility-rules/nullable.js +12 -0
  74. package/rules/utility-rules/one-of.d.ts +1 -1
  75. package/rules/utility-rules/one-of.js +2 -2
  76. package/rules/utility-rules/optional.d.ts +2 -2
  77. package/rules/utility-rules/optional.js +2 -2
  78. package/rules/utility-rules/pipe.js +1 -1
  79. package/rules/utility-rules/required.js +1 -1
  80. package/rules/utility-rules/string-utils.d.ts +2 -2
  81. package/rules/utility-rules/string-utils.js +17 -15
package/rules/index.d.ts CHANGED
@@ -44,6 +44,7 @@ export * from './type-rules/is-bigint.js';
44
44
  export * from './type-rules/is-boolean.js';
45
45
  export * from './type-rules/is-date.js';
46
46
  export * from './type-rules/is-enum.js';
47
+ export * from './type-rules/is-instanceof.js';
47
48
  export * from './type-rules/is-integer.js';
48
49
  export * from './type-rules/is-null.js';
49
50
  export * from './type-rules/is-number.js';
@@ -54,7 +55,9 @@ export * from './type-rules/is-tuple.js';
54
55
  export * from './type-rules/is-undefined.js';
55
56
  export * from './utility-rules/all-of.js';
56
57
  export * from './utility-rules/exists.js';
58
+ export * from './utility-rules/fixed.js';
57
59
  export * from './utility-rules/get-length.js';
60
+ export * from './utility-rules/nullable.js';
58
61
  export * from './utility-rules/one-of.js';
59
62
  export * from './utility-rules/optional.js';
60
63
  export * from './utility-rules/pipe.js';
package/rules/index.js CHANGED
@@ -44,6 +44,7 @@ export * from './type-rules/is-bigint.js';
44
44
  export * from './type-rules/is-boolean.js';
45
45
  export * from './type-rules/is-date.js';
46
46
  export * from './type-rules/is-enum.js';
47
+ export * from './type-rules/is-instanceof.js';
47
48
  export * from './type-rules/is-integer.js';
48
49
  export * from './type-rules/is-null.js';
49
50
  export * from './type-rules/is-number.js';
@@ -54,7 +55,9 @@ export * from './type-rules/is-tuple.js';
54
55
  export * from './type-rules/is-undefined.js';
55
56
  export * from './utility-rules/all-of.js';
56
57
  export * from './utility-rules/exists.js';
58
+ export * from './utility-rules/fixed.js';
57
59
  export * from './utility-rules/get-length.js';
60
+ export * from './utility-rules/nullable.js';
58
61
  export * from './utility-rules/one-of.js';
59
62
  export * from './utility-rules/optional.js';
60
63
  export * from './utility-rules/pipe.js';
@@ -4,7 +4,7 @@ import { validator, } from '../../core/index.js';
4
4
  * @validator isDefined
5
5
  */
6
6
  export function isDefined(options) {
7
- return validator('is-defined', (input, context, _this) => {
7
+ return validator(isDefined.name, (input, context, _this) => {
8
8
  if (input !== undefined)
9
9
  return input;
10
10
  context.fail(_this, `Value must be defined`, input);
@@ -4,7 +4,7 @@ import { validator, } from '../../core/index.js';
4
4
  * @validator isEmpty
5
5
  */
6
6
  export function isEmpty(options) {
7
- return validator('isEmpty', (input, context, _this) => {
7
+ return validator(isEmpty.name, (input, context, _this) => {
8
8
  if (input == null)
9
9
  return input;
10
10
  if (typeof input === 'string') {
@@ -4,7 +4,7 @@ import { validator, } from '../../core/index.js';
4
4
  * @validator isEqual
5
5
  */
6
6
  export function isEqual(compare, options) {
7
- return validator('isEqual', (input, context, _this) => {
7
+ return validator(isEqual.name, (input, context, _this) => {
8
8
  if (input == null)
9
9
  return input;
10
10
  if (input !== compare)
@@ -17,7 +17,7 @@ export function isEqual(compare, options) {
17
17
  * @validator isNotEqual
18
18
  */
19
19
  export function isNotEqual(compare, options) {
20
- return validator('isNotEqual', (input, context, _this) => {
20
+ return validator(isNotEqual.name, (input, context, _this) => {
21
21
  if (input == null)
22
22
  return input;
23
23
  if (input === compare) {
@@ -4,7 +4,7 @@ import { validator, } from '../../core/index.js';
4
4
  * @validator iGt
5
5
  */
6
6
  export function isGt(minValue, options) {
7
- return validator('isGt', (input, context, _this) => {
7
+ return validator(isGt.name, (input, context, _this) => {
8
8
  if ((typeof minValue === 'number' || typeof minValue === 'bigint') &&
9
9
  (typeof input === 'number' || typeof input === 'bigint') &&
10
10
  input > minValue) {
@@ -4,7 +4,7 @@ import type { range } from './range.js';
4
4
  * Checks if value is grater than or equal to minValue
5
5
  * @validator isGte
6
6
  */
7
- export declare function isGte<T extends range.Input>(minValue: T, options?: isGte.Options): Validator<T, T>;
7
+ export declare function isGte<T extends range.Input>(minValue: T, options?: isGte.Options): Validator<T>;
8
8
  export declare namespace isGte {
9
9
  interface Options extends ValidationOptions {
10
10
  caseInsensitive?: boolean;
@@ -4,7 +4,7 @@ import { validator, } from '../../core/index.js';
4
4
  * @validator isGte
5
5
  */
6
6
  export function isGte(minValue, options) {
7
- return validator('isGte', (input, context, _this) => {
7
+ return validator(isGte.name, (input, context, _this) => {
8
8
  if ((typeof minValue === 'number' || typeof minValue === 'bigint') &&
9
9
  (typeof input === 'number' || typeof input === 'bigint') &&
10
10
  input >= minValue) {
@@ -4,7 +4,7 @@ import { validator, } from '../../core/index.js';
4
4
  * @validator isLt
5
5
  */
6
6
  export function isLt(maxValue, options) {
7
- return validator('isLt', (input, context, _this) => {
7
+ return validator(isLt.name, (input, context, _this) => {
8
8
  if ((typeof maxValue === 'number' || typeof maxValue === 'bigint') &&
9
9
  (typeof input === 'number' || typeof input === 'bigint') &&
10
10
  input < maxValue) {
@@ -4,7 +4,7 @@ import { validator, } from '../../core/index.js';
4
4
  * @validator isLte
5
5
  */
6
6
  export function isLte(maxValue, options) {
7
- return validator('isLte', (input, context, _this) => {
7
+ return validator(isLte.name, (input, context, _this) => {
8
8
  if ((typeof maxValue === 'number' || typeof maxValue === 'bigint') &&
9
9
  (typeof input === 'number' || typeof input === 'bigint') &&
10
10
  input <= maxValue) {
@@ -4,7 +4,7 @@ import { validator, } from '../../core/index.js';
4
4
  * @validator range
5
5
  */
6
6
  export function range(minValue, maxValue, options) {
7
- return validator('range', (input, context, _this) => {
7
+ return validator(range.name, (input, context, _this) => {
8
8
  if ((typeof minValue === 'number' || typeof minValue === 'bigint') &&
9
9
  (typeof maxValue === 'number' || typeof maxValue === 'bigint') &&
10
10
  (typeof input === 'number' || typeof input === 'bigint') &&
@@ -3,4 +3,4 @@ import { validator } from '../../core/index.js';
3
3
  * Does nothing, just returns the original input value.
4
4
  * @validator isAny
5
5
  */
6
- export const isAny = () => validator('is-any', (input) => input);
6
+ export const isAny = () => validator(isAny.name, (input) => input);
@@ -1,4 +1,4 @@
1
- import { type ValidationOptions, type Validator } from '../../core/index.js';
1
+ import type { ValidationOptions, Validator } from '../../core/index.js';
2
2
  /**
3
3
  * Validates if the value is "array" and applies validation for each item.
4
4
  * Converts input value to array if the coerce option is set to 'true'.
@@ -1,11 +1,11 @@
1
- import { validator, } from '../../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  /**
3
3
  * Validates if the value is "array" and applies validation for each item.
4
4
  * Converts input value to array if the coerce option is set to 'true'.
5
5
  * @validator isArray
6
6
  */
7
7
  export function isArray(itemValidator, options) {
8
- return validator('isArray', (input, context, _this) => {
8
+ return validator(isArray.name, (input, context, _this) => {
9
9
  const coerce = options?.coerce ?? context.coerce;
10
10
  let output = input;
11
11
  if (output != null && coerce && !Array.isArray(output))
@@ -5,7 +5,7 @@ import { validator, } from '../../core/index.js';
5
5
  * @validator isNumber
6
6
  */
7
7
  export function isBigint(options) {
8
- return validator('isBigint', (input, context, _this) => {
8
+ return validator(isBigint.name, (input, context, _this) => {
9
9
  const coerce = options?.coerce ?? context.coerce;
10
10
  if (typeof input === 'bigint')
11
11
  return input;
@@ -7,7 +7,7 @@ const FALSE_PATTERN = /^false|f|0|no|n$/i;
7
7
  * @validator isBoolean
8
8
  */
9
9
  export function isBoolean(options) {
10
- return validator('isBoolean', (input, context, _this) => {
10
+ return validator(isBoolean.name, (input, context, _this) => {
11
11
  const coerce = options?.coerce ?? context.coerce;
12
12
  let output = input;
13
13
  if (output != null && typeof output !== 'boolean' && coerce) {
@@ -1,4 +1,5 @@
1
1
  import * as datefns from 'date-fns';
2
+ import {} from 'ts-gems';
2
3
  import { validator, } from '../../core/index.js';
3
4
  /**
4
5
  * Validates if value is a "Date" instance or ISO 8601 formatted date string.
@@ -7,7 +8,7 @@ import { validator, } from '../../core/index.js';
7
8
  */
8
9
  export function isDate(options) {
9
10
  const trim = options?.trim;
10
- return validator('isDate', (input, context, _this) => {
11
+ return validator(isDate.name, (input, context, _this) => {
11
12
  const coerce = options?.coerce ?? context.coerce;
12
13
  let d;
13
14
  if (input instanceof Date)
@@ -43,7 +44,7 @@ export function isDateString(options) {
43
44
  let precisionMin = options?.precisionMin || 'minutes';
44
45
  const precisionMinIdx = Math.min(precisionMaxIdx, PRECISION_INDEX[precisionMin] || 6);
45
46
  precisionMin = (PRECISION_INDEX_KEYS[PRECISION_INDEX_VALUES.indexOf(precisionMinIdx)] || precisionMin);
46
- return validator('isDateString', (input, context, _this) => {
47
+ return validator(isDateString.name, (input, context, _this) => {
47
48
  const coerce = options?.coerce ?? context.coerce;
48
49
  const parsed = coerceDateString(input, trim ? precisionMax : undefined);
49
50
  if (parsed) {
@@ -22,7 +22,7 @@ export function isEnum(values, options) {
22
22
  a[v] = true;
23
23
  return a;
24
24
  }, {});
25
- return validator('isEnum', (input, context, _this) => {
25
+ return validator(isEnum.name, (input, context, _this) => {
26
26
  if (input != null &&
27
27
  valObj[typeof input === 'string' && caseInSensitive
28
28
  ? input.toUpperCase()
@@ -0,0 +1,11 @@
1
+ import type { Type } from 'ts-gems';
2
+ import { type ValidationOptions } from '../../core/index.js';
3
+ /**
4
+ * Validates if the value instance of given class or classes
5
+ * @validator isUndefined
6
+ */
7
+ export declare function isInstanceOf<T extends object>(clazz: Type<T>, options?: isInstanceOf.Options): import("../../core/validator.js").Validator<T, unknown, import("../../core/types.js").ExecutionOptions>;
8
+ export declare namespace isInstanceOf {
9
+ interface Options extends ValidationOptions {
10
+ }
11
+ }
@@ -0,0 +1,17 @@
1
+ import { isPlainObject } from '@jsopen/objects';
2
+ import { validator, } from '../../core/index.js';
3
+ /**
4
+ * Validates if the value instance of given class or classes
5
+ * @validator isUndefined
6
+ */
7
+ export function isInstanceOf(clazz, options) {
8
+ return validator(isInstanceOf.name, (input, context, _this) => {
9
+ if ((options?.coerce ?? context.coerce) && isPlainObject(input)) {
10
+ Object.setPrototypeOf(input, clazz.prototype);
11
+ }
12
+ if (!(input instanceof clazz)) {
13
+ context.fail(_this, `Value must be an instance of "${clazz.name}"`, input);
14
+ }
15
+ return input;
16
+ }, options);
17
+ }
@@ -5,7 +5,7 @@ import { validator, } from '../../core/index.js';
5
5
  * @validator isInteger
6
6
  */
7
7
  export function isInteger(options) {
8
- return validator('isInteger', (input, context, _this) => {
8
+ return validator(isInteger.name, (input, context, _this) => {
9
9
  const coerce = options?.coerce ?? context.coerce;
10
10
  let output = input;
11
11
  if (output != null && typeof output !== 'number' && coerce) {
@@ -4,7 +4,7 @@ import { validator, } from '../../core/index.js';
4
4
  * @validator isNull
5
5
  */
6
6
  export function isNull(options) {
7
- return validator('isNull', (input, context, _this) => {
7
+ return validator(isNull.name, (input, context, _this) => {
8
8
  if (input === null)
9
9
  return input;
10
10
  context.fail(_this, `Value must be null`, input);
@@ -15,7 +15,7 @@ export function isNull(options) {
15
15
  * @validator isNotNull
16
16
  */
17
17
  export function isNotNull(options) {
18
- return validator('isNotNull', (input, context, _this) => {
18
+ return validator(isNotNull.name, (input, context, _this) => {
19
19
  if (input !== null)
20
20
  return input;
21
21
  context.fail(_this, `{{label}} is null`, input);
@@ -26,7 +26,7 @@ export function isNotNull(options) {
26
26
  * @validator isNullish
27
27
  */
28
28
  export function isNullish(options) {
29
- return validator('isNullish', (input, context, _this) => {
29
+ return validator(isNullish.name, (input, context, _this) => {
30
30
  if (input == null)
31
31
  return input;
32
32
  context.fail(_this, `{{label}} is not nullish`, input);
@@ -37,7 +37,7 @@ export function isNullish(options) {
37
37
  * @validator isNotNullish
38
38
  */
39
39
  export function isNotNullish(options) {
40
- return validator('isNotNullish', (input, context, _this) => {
40
+ return validator(isNotNullish.name, (input, context, _this) => {
41
41
  if (input != null)
42
42
  return input;
43
43
  if (input === null)
@@ -5,7 +5,7 @@ import { validator, } from '../../core/index.js';
5
5
  * @validator isNumber
6
6
  */
7
7
  export function isNumber(options) {
8
- return validator('isNumber', (input, context, _this) => {
8
+ return validator(isNumber.name, (input, context, _this) => {
9
9
  const coerce = options?.coerce ?? context.coerce;
10
10
  let output = input;
11
11
  if (output != null && typeof output !== 'number' && coerce) {
@@ -33,7 +33,7 @@ export function isObject(schema, options) {
33
33
  throw new TypeError(`Invalid definition in validation schema (${k})`);
34
34
  }
35
35
  }
36
- const _rule = validator('isObject', (input, context, _this) => {
36
+ const _rule = validator(isObject.name, (input, context, _this) => {
37
37
  let output = input;
38
38
  if (ctor && ctor[preValidation]) {
39
39
  output = ctor[preValidation](output, context, _this);
@@ -1,4 +1,4 @@
1
- import { type ValidationOptions, type Validator } from '../../core/index.js';
1
+ import type { ValidationOptions, Validator } from '../../core/index.js';
2
2
  /**
3
3
  * Validates the record object according to given "key" and "value" rules
4
4
  * Converts properties according to rules if the coerce option is set to 'true'.
@@ -1,11 +1,11 @@
1
- import { validator, } from '../../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  /**
3
3
  * Validates the record object according to given "key" and "value" rules
4
4
  * Converts properties according to rules if the coerce option is set to 'true'.
5
5
  * @validator isRecord
6
6
  */
7
7
  export function isRecord(keyRule, valueRule, options) {
8
- return validator('isRecord', (input, context, _this) => {
8
+ return validator(isRecord.name, (input, context, _this) => {
9
9
  if (!(input && typeof input === 'object')) {
10
10
  context.fail(_this, `Value must be an object`, input);
11
11
  return;
@@ -5,7 +5,7 @@ import { validator, } from '../../core/index.js';
5
5
  * @validator isString
6
6
  */
7
7
  export function isString(options) {
8
- return validator('isString', (input, context, _this) => {
8
+ return validator(isString.name, (input, context, _this) => {
9
9
  const coerce = options?.coerce ?? context.coerce;
10
10
  let output = input;
11
11
  if (output != null && typeof output !== 'string' && coerce) {
@@ -1,6 +1,6 @@
1
1
  import { validator, } from '../../core/index.js';
2
2
  export function isTuple(items, options) {
3
- return validator('isTuple', (input, context, _this) => {
3
+ return validator(isTuple.name, (input, context, _this) => {
4
4
  const coerce = options?.coerce ?? context.coerce;
5
5
  let output = input;
6
6
  if (output != null && coerce && !Array.isArray(output))
@@ -4,7 +4,7 @@ import { validator, } from '../../core/index.js';
4
4
  * @validator isUndefined
5
5
  */
6
6
  export function isUndefined(options) {
7
- return validator('isUndefined', (input, context, _this) => {
7
+ return validator(isUndefined.name, (input, context, _this) => {
8
8
  if (options?.coerce ?? context.coerce)
9
9
  return undefined;
10
10
  if (input === undefined)
@@ -1,4 +1,4 @@
1
- import { ValidationOptions, type Validator } from '../../core/index.js';
1
+ import type { ValidationOptions, Validator } from '../../core/index.js';
2
2
  /**
3
3
  * Test given value against to all codecs and returns original input
4
4
  * @validator allOf
@@ -1,10 +1,10 @@
1
- import { validator, } from '../../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  /**
3
3
  * Test given value against to all codecs and returns original input
4
4
  * @validator allOf
5
5
  */
6
6
  export function allOf(rules, options) {
7
- return validator('allOf', (input, context) => {
7
+ return validator(allOf.name, (input, context) => {
8
8
  let i;
9
9
  let c;
10
10
  const l = rules.length;
@@ -4,7 +4,7 @@ import { validator, } from '../../core/index.js';
4
4
  * @validator exists
5
5
  */
6
6
  export function exists(options) {
7
- return validator('exists', (input, context, _this) => {
7
+ return validator(exists.name, (input, context, _this) => {
8
8
  if (input !== undefined ||
9
9
  (context.scope &&
10
10
  Object.getOwnPropertyDescriptor(context.scope, input))) {
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Check if value is not nullish before calling nested rule
3
+ * @validator required
4
+ */
5
+ export declare function fixed<T, I>(value: T): import("../../core/validator.js").Validator<T, I, import("../../core/types.js").ExecutionOptions>;
@@ -0,0 +1,10 @@
1
+ import { validator } from '../../core/index.js';
2
+ /**
3
+ * Check if value is not nullish before calling nested rule
4
+ * @validator required
5
+ */
6
+ export function fixed(value) {
7
+ return validator(fixed.name, () => {
8
+ return value;
9
+ });
10
+ }
@@ -4,7 +4,7 @@ import { validator } from '../../core/index.js';
4
4
  * @validator getLength
5
5
  */
6
6
  export function getLength() {
7
- return validator('getLength', (input, context, _this) => {
7
+ return validator(getLength.name, (input, context, _this) => {
8
8
  if (typeof input === 'string')
9
9
  return input.length;
10
10
  if (Array.isArray(input))
@@ -0,0 +1,11 @@
1
+ import type { Nullish } from 'ts-gems';
2
+ import { type ValidationOptions, type Validator } from '../../core/index.js';
3
+ /**
4
+ * Makes the sub-rule nullable (undefined of null)
5
+ * @validator optional
6
+ */
7
+ export declare function nullable<T, I>(nested: Validator<T, I>, options?: nullable.Options): Validator<Nullish<T>, Nullish<I>, import("../../core/types.js").ExecutionOptions>;
8
+ export declare namespace nullable {
9
+ interface Options extends ValidationOptions {
10
+ }
11
+ }
@@ -0,0 +1,12 @@
1
+ import { validator, } from '../../core/index.js';
2
+ /**
3
+ * Makes the sub-rule nullable (undefined of null)
4
+ * @validator optional
5
+ */
6
+ export function nullable(nested, options) {
7
+ return validator(nullable.name, (input, context) => {
8
+ if (input == null)
9
+ return input;
10
+ return nested(input, context);
11
+ }, options);
12
+ }
@@ -1,4 +1,4 @@
1
- import { ValidationOptions, type Validator } from '../../core/index.js';
1
+ import type { ValidationOptions, Validator } from '../../core/index.js';
2
2
  type DiscriminatorRecord = Record<string, Validator>;
3
3
  /**
4
4
  *
@@ -1,10 +1,10 @@
1
- import { validator, } from '../../core/index.js';
1
+ import { validator } from '../../core/index.js';
2
2
  /**
3
3
  *
4
4
  */
5
5
  export function oneOf(rules, options) {
6
6
  const l = rules.length;
7
- return validator('union', (input, context, _this) => {
7
+ return validator(oneOf.name, (input, context, _this) => {
8
8
  let i;
9
9
  let c;
10
10
  let discriminator;
@@ -1,10 +1,10 @@
1
- import type { Nullish } from 'ts-gems';
1
+ import type { Maybe } from 'ts-gems';
2
2
  import { type ValidationOptions, type Validator } from '../../core/index.js';
3
3
  /**
4
4
  * Makes the sub-rule optional
5
5
  * @validator optional
6
6
  */
7
- export declare function optional<T, I>(nested: Validator<T, I>, options?: optional.Options): Validator<Nullish<T>, Nullish<I>, import("../../core/types.js").ExecutionOptions>;
7
+ export declare function optional<T, I>(nested: Validator<T, I>, options?: optional.Options): Validator<Maybe<T>, Maybe<I>, import("../../core/types.js").ExecutionOptions>;
8
8
  export declare namespace optional {
9
9
  interface Options extends ValidationOptions {
10
10
  }
@@ -4,8 +4,8 @@ import { validator, } from '../../core/index.js';
4
4
  * @validator optional
5
5
  */
6
6
  export function optional(nested, options) {
7
- return validator('optional', (input, context) => {
8
- if (input == null)
7
+ return validator(optional.name, (input, context) => {
8
+ if (input === undefined)
9
9
  return input;
10
10
  return nested(input, context);
11
11
  }, options);
@@ -6,7 +6,7 @@ import { validator, } from '../../core/index.js';
6
6
  export function pipe(rules, options) {
7
7
  const l = rules.length;
8
8
  const returnIndex = options?.returnIndex;
9
- return validator('pipe', (input, context) => {
9
+ return validator(pipe.name, (input, context) => {
10
10
  let i;
11
11
  let c;
12
12
  let v = input;
@@ -4,7 +4,7 @@ import { validator, } from '../../core/index.js';
4
4
  * @validator required
5
5
  */
6
6
  export function required(nested, options) {
7
- return validator('required', (input, context, _this) => {
7
+ return validator(required.name, (input, context, _this) => {
8
8
  if (input == null)
9
9
  input = options?.default;
10
10
  if (input == null) {
@@ -27,9 +27,9 @@ export declare function trim(): import("../../core/validator.js").Validator<stri
27
27
  * Removes whitespace from the end of a string
28
28
  * @validator trimEnd
29
29
  */
30
- export declare const trimEnd: () => import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
30
+ export declare function trimEnd(): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
31
31
  /**
32
32
  * Removes whitespace from the beginning of a string
33
33
  * @validator trimStart
34
34
  */
35
- export declare const trimStart: () => import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
35
+ export declare function trimStart(): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
@@ -1,13 +1,13 @@
1
1
  import { validator } from '../../core/index.js';
2
2
  export function stringReplace(searchValue, replacer) {
3
- return validator('stringReplace', (input) => {
3
+ return validator(stringReplace.name, (input) => {
4
4
  if (input == null)
5
5
  return input;
6
6
  return String(input).replace(searchValue, replacer);
7
7
  });
8
8
  }
9
9
  export function stringSplit(splitter, limit) {
10
- return validator('stringSplit', (input) => {
10
+ return validator(stringSplit.name, (input) => {
11
11
  if (input == null)
12
12
  return input;
13
13
  return String(input).split(splitter, limit);
@@ -18,7 +18,7 @@ export function stringSplit(splitter, limit) {
18
18
  * @validator trim
19
19
  */
20
20
  export function trim() {
21
- return validator('trim', (input) => {
21
+ return validator(trim.name, (input) => {
22
22
  if (input == null)
23
23
  return input;
24
24
  return String(input).trim();
@@ -29,20 +29,22 @@ export function trim() {
29
29
  * Removes whitespace from the end of a string
30
30
  * @validator trimEnd
31
31
  */
32
- export const trimEnd = () => trimEndRule;
33
- const trimEndRule = validator('trimEnd', (input) => {
34
- if (input == null)
35
- return input;
36
- return String(input).trimEnd();
37
- });
32
+ export function trimEnd() {
33
+ return validator(trimEnd.name, (input) => {
34
+ if (input == null)
35
+ return input;
36
+ return String(input).trimEnd();
37
+ });
38
+ }
38
39
  // *************************************************************
39
40
  /**
40
41
  * Removes whitespace from the beginning of a string
41
42
  * @validator trimStart
42
43
  */
43
- export const trimStart = () => trimStartRule;
44
- const trimStartRule = validator('trimStart', (input) => {
45
- if (input == null)
46
- return input;
47
- return String(input).trimStart();
48
- });
44
+ export function trimStart() {
45
+ return validator(trimStart.name, (input) => {
46
+ if (input == null)
47
+ return input;
48
+ return String(input).trimStart();
49
+ });
50
+ }