justus 0.1.5 → 0.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 (116) hide show
  1. package/dist/{dts-generator.js → dts-generator.cjs} +3 -3
  2. package/dist/{dts-generator.js.map → dts-generator.cjs.map} +1 -1
  3. package/{dts-generator.d.ts → dist/dts-generator.d.ts} +1 -1
  4. package/dist/dts-generator.mjs +1 -1
  5. package/dist/dts-generator.mjs.map +1 -1
  6. package/dist/errors.cjs +113 -0
  7. package/dist/errors.cjs.map +6 -0
  8. package/dist/errors.d.ts +55 -0
  9. package/dist/errors.mjs +85 -0
  10. package/dist/errors.mjs.map +6 -0
  11. package/dist/index.cjs +133 -0
  12. package/dist/index.cjs.map +6 -0
  13. package/dist/index.d.ts +47 -0
  14. package/dist/index.mjs +23 -818
  15. package/dist/index.mjs.map +3 -3
  16. package/dist/schema.cjs +44 -0
  17. package/dist/schema.cjs.map +6 -0
  18. package/dist/schema.d.ts +16 -0
  19. package/dist/schema.mjs +18 -0
  20. package/dist/schema.mjs.map +6 -0
  21. package/dist/types.cjs +67 -0
  22. package/dist/types.cjs.map +6 -0
  23. package/dist/types.d.ts +136 -0
  24. package/dist/types.mjs +36 -0
  25. package/dist/types.mjs.map +6 -0
  26. package/dist/utilities.cjs +54 -0
  27. package/dist/utilities.cjs.map +6 -0
  28. package/dist/utilities.d.ts +7 -0
  29. package/dist/utilities.mjs +29 -0
  30. package/dist/utilities.mjs.map +6 -0
  31. package/dist/validators/any.cjs +39 -0
  32. package/dist/validators/any.cjs.map +6 -0
  33. package/dist/validators/any.d.ts +7 -0
  34. package/dist/validators/any.mjs +13 -0
  35. package/dist/validators/any.mjs.map +6 -0
  36. package/dist/validators/array.cjs +104 -0
  37. package/dist/validators/array.cjs.map +6 -0
  38. package/dist/validators/array.d.ts +32 -0
  39. package/dist/validators/array.mjs +76 -0
  40. package/dist/validators/array.mjs.map +6 -0
  41. package/dist/validators/boolean.cjs +56 -0
  42. package/dist/validators/boolean.cjs.map +6 -0
  43. package/dist/validators/boolean.d.ts +21 -0
  44. package/dist/validators/boolean.mjs +30 -0
  45. package/dist/validators/boolean.mjs.map +6 -0
  46. package/dist/validators/constant.cjs +51 -0
  47. package/dist/validators/constant.cjs.map +6 -0
  48. package/dist/validators/constant.d.ts +12 -0
  49. package/dist/validators/constant.mjs +24 -0
  50. package/dist/validators/constant.mjs.map +6 -0
  51. package/dist/validators/date.cjs +80 -0
  52. package/dist/validators/date.cjs.map +6 -0
  53. package/dist/validators/date.d.ts +22 -0
  54. package/dist/validators/date.mjs +54 -0
  55. package/dist/validators/date.mjs.map +6 -0
  56. package/dist/validators/never.cjs +44 -0
  57. package/dist/validators/never.cjs.map +6 -0
  58. package/dist/validators/never.d.ts +9 -0
  59. package/dist/validators/never.mjs +18 -0
  60. package/dist/validators/never.mjs.map +6 -0
  61. package/dist/validators/number.cjs +153 -0
  62. package/dist/validators/number.cjs.map +6 -0
  63. package/dist/validators/number.d.ts +48 -0
  64. package/dist/validators/number.mjs +126 -0
  65. package/dist/validators/number.mjs.map +6 -0
  66. package/dist/validators/object.cjs +129 -0
  67. package/dist/validators/object.cjs.map +6 -0
  68. package/dist/validators/object.d.ts +22 -0
  69. package/dist/validators/object.mjs +108 -0
  70. package/dist/validators/object.mjs.map +6 -0
  71. package/dist/validators/optional.cjs +61 -0
  72. package/dist/validators/optional.cjs.map +6 -0
  73. package/dist/validators/optional.d.ts +21 -0
  74. package/dist/validators/optional.mjs +35 -0
  75. package/dist/validators/optional.mjs.map +6 -0
  76. package/dist/validators/string.cjs +84 -0
  77. package/dist/validators/string.cjs.map +6 -0
  78. package/dist/validators/string.d.ts +35 -0
  79. package/dist/validators/string.mjs +57 -0
  80. package/dist/validators/string.mjs.map +6 -0
  81. package/dist/validators/tuple.cjs +95 -0
  82. package/dist/validators/tuple.cjs.map +6 -0
  83. package/dist/validators/tuple.d.ts +15 -0
  84. package/dist/validators/tuple.mjs +69 -0
  85. package/dist/validators/tuple.mjs.map +6 -0
  86. package/dist/validators/union.cjs +78 -0
  87. package/dist/validators/union.cjs.map +6 -0
  88. package/dist/validators/union.d.ts +22 -0
  89. package/dist/validators/union.mjs +50 -0
  90. package/dist/validators/union.mjs.map +6 -0
  91. package/dist/validators/url.cjs +117 -0
  92. package/dist/validators/url.cjs.map +6 -0
  93. package/dist/validators/url.d.ts +57 -0
  94. package/dist/validators/url.mjs +91 -0
  95. package/dist/validators/url.mjs.map +6 -0
  96. package/package.json +33 -23
  97. package/src/dts-generator.ts +4 -0
  98. package/src/index.ts +23 -23
  99. package/src/schema.ts +7 -5
  100. package/src/types.ts +51 -32
  101. package/src/utilities.ts +6 -2
  102. package/src/validators/array.ts +6 -5
  103. package/src/validators/boolean.ts +2 -2
  104. package/src/validators/constant.ts +3 -1
  105. package/src/validators/date.ts +1 -1
  106. package/src/validators/never.ts +4 -2
  107. package/src/validators/number.ts +8 -7
  108. package/src/validators/object.ts +12 -12
  109. package/src/validators/optional.ts +5 -8
  110. package/src/validators/string.ts +8 -6
  111. package/src/validators/tuple.ts +5 -3
  112. package/src/validators/union.ts +7 -5
  113. package/src/validators/url.ts +8 -7
  114. package/dist/index.js +0 -949
  115. package/dist/index.js.map +0 -6
  116. package/index.d.ts +0 -617
package/src/types.ts CHANGED
@@ -22,12 +22,21 @@ export const additionalValidator = Symbol.for('justus.additionalValidator')
22
22
  * Options to be using while validating.
23
23
  */
24
24
  export interface ValidationOptions {
25
- /** Strip additional, undeclared properties from objects */
26
- readonly stripAdditionalProperties: boolean,
27
- /** Strip `null`s from an object when associated with an optional key */
28
- readonly stripOptionalNulls: boolean,
29
- /** Ignore and strip forbidden (`never`) properties from objects */
30
- readonly stripForbiddenProperties: boolean,
25
+ /** Strip additional, undeclared properties from objects (default: `false`) */
26
+ stripAdditionalProperties?: boolean,
27
+ /** Strip `null`s from an object when associated with an optional key (default: `false`) */
28
+ stripOptionalNulls?: boolean,
29
+ /** Ignore and strip forbidden (`never`) properties from objects (default: `false`) */
30
+ stripForbiddenProperties?: boolean,
31
+ }
32
+
33
+ /**
34
+ * Default validation options.
35
+ */
36
+ export const defaultValidationOptions: Readonly<Required<ValidationOptions>> = {
37
+ stripAdditionalProperties: false,
38
+ stripForbiddenProperties: false,
39
+ stripOptionalNulls: false,
31
40
  }
32
41
 
33
42
  /**
@@ -40,7 +49,7 @@ export interface Validator<T = any> extends Iterable<TupleRestParameter<T>> {
40
49
  optional?: boolean
41
50
 
42
51
  /** Validate a _value_ and optionally convert it to the required `Type` */
43
- validate(value: unknown, options: ValidationOptions): T
52
+ validate(value: unknown, options?: ValidationOptions | undefined): T
44
53
 
45
54
  /** Allow any `Validator` to be used as a rest parameter in `Tuple`s */
46
55
  [Symbol.iterator](): Generator<TupleRestParameter<T>>;
@@ -68,13 +77,14 @@ export function makeValidatorFactory<
68
77
  * A `Validator` is an object capable of validating a given _value_ and
69
78
  * (possibly) converting it the required type `T`.
70
79
  */
71
- export abstract class AbstractValidator<T = any> implements Iterable<TupleRestParameter<T>> {
80
+ export abstract class AbstractValidator<T = any>
81
+ implements Validator<T>, Iterable<TupleRestParameter<T>> {
72
82
  [isValidator]: true = true
73
83
 
74
84
  optional?: boolean = undefined
75
85
 
76
86
  /** Validate a _value_ and optionally convert it to the required `Type` */
77
- abstract validate(value: unknown, options: ValidationOptions): T
87
+ abstract validate(value: unknown, options?: ValidationOptions | undefined): T
78
88
 
79
89
  /** Allow any `Validator` to be used as a rest parameter in `Tuple`s */
80
90
  * [Symbol.iterator](): Generator<TupleRestParameter<T>> {
@@ -101,23 +111,21 @@ export type Validation =
101
111
  * Infer the type returned by a `Validation` when validating.
102
112
  */
103
113
  export type InferValidation<V> =
104
- // Let `InferValidationType<T>` be liberal in the type it accepts and check
105
- // here whether it extends `Validation`
106
- V extends Validation ?
107
- // Validators return their validation type
108
- V extends Validator<infer T> ? T :
109
-
110
- // Tuples and schemas are inferred using their own types
111
- V extends Tuple ? InferTuple<V> :
112
- V extends Schema ? InferSchema<V> :
113
-
114
- // Primitives are returned as constants
115
- V extends boolean ? V :
116
- V extends number ? V :
117
- V extends string ? V :
118
- V extends null ? V :
119
- never :
120
- never
114
+ // Validators return their validation type
115
+ V extends Validator<infer T> ? T :
116
+
117
+ // Primitives are returned as constants
118
+ V extends undefined ? V :
119
+ V extends boolean ? V :
120
+ V extends number ? V :
121
+ V extends string ? V :
122
+ V extends null ? V :
123
+
124
+ // Tuples are inferred using their own types
125
+ V extends Tuple ? InferTuple<V> :
126
+
127
+ // Anyhing else can only be a schema
128
+ InferSchema<V>
121
129
 
122
130
 
123
131
  /* ========================================================================== *
@@ -200,17 +208,28 @@ export interface AdditionalProperties<V extends Validator | false> {
200
208
  * ========================================================================== */
201
209
 
202
210
  /** Infer the type validated by a `Schema` */
203
- export type InferSchema<S extends Schema> =
211
+ export type InferSchema<S> =
204
212
  S extends AdditionalProperties<Validator<infer V>> ?
205
213
  { [ key in string ] : V } & InferSchema2<S> :
206
214
  InferSchema2<S>
207
215
 
208
216
  /** Infer the property types described by a `Schema` */
209
- export type InferSchema2<S extends Schema> =
210
- { [ key in keyof S as key extends string ? key : never ]:
211
- InferValidation<S[key]>
212
- }
213
-
217
+ export type InferSchema2<S> = {
218
+ // this first part of the type infers all keys from the schema into their
219
+ // type, but makes *each* key optional... we'll restrict in the next part...
220
+ [ key in keyof S as key extends string ? key : never ] ? : InferValidation<S[key]>
221
+ } & {
222
+ // this second part infers *only* keys that _do not_ contain a "undefined"
223
+ // in their unions, and associates them with the inferred value, basically
224
+ // making the key *non optional*
225
+ [ key in keyof S as
226
+ key extends string ?
227
+ undefined extends InferValidation<S[key]> ?
228
+ never :
229
+ key :
230
+ never ] -? :
231
+ InferValidation<S[key]>
232
+ }
214
233
 
215
234
  /* ========================================================================== *
216
235
  * TYPE BRANDING *
package/src/utilities.ts CHANGED
@@ -1,7 +1,11 @@
1
+ import { isValidator, schemaValidator } from './types'
1
2
  import { ConstantValidator, nullValidator } from './validators/constant'
2
- import { isValidator, Schema, schemaValidator, Validation, Validator } from './types'
3
- import { TupleValidator } from './validators/tuple'
3
+ // eslint-disable-next-line import/no-cycle
4
4
  import { ObjectValidator } from './validators/object'
5
+ // eslint-disable-next-line import/no-cycle
6
+ import { TupleValidator } from './validators/tuple'
7
+
8
+ import type { Schema, Validation, Validator } from './types'
5
9
 
6
10
  /* ========================================================================== *
7
11
  * UTILITY FUNCTIONS *
@@ -1,8 +1,10 @@
1
1
  import { assertSchema, assertValidation, ValidationErrorBuilder } from '../errors'
2
- import { InferValidation, Validation, ValidationOptions, AbstractValidator, Validator, makeValidatorFactory } from '../types'
2
+ import { AbstractValidator, makeValidatorFactory } from '../types'
3
3
  import { getValidator } from '../utilities'
4
4
  import { any } from './any'
5
5
 
6
+ import type { InferValidation, Validation, ValidationOptions, Validator } from '../types'
7
+
6
8
  /* ========================================================================== *
7
9
  * ARRAYS VALIDATION *
8
10
  * ========================================================================== */
@@ -21,8 +23,7 @@ export interface ArrayConstraints<V extends Validation> {
21
23
 
22
24
  /** Basic validator for `Array` instances. */
23
25
  export class AnyArrayValidator<T = any> extends AbstractValidator<T[]> {
24
- validate(value: unknown, options: ValidationOptions): T[] {
25
- void options
26
+ validate(value: unknown): T[] {
26
27
  assertValidation(Array.isArray(value), 'Value is not an "array"')
27
28
  return [ ...value ]
28
29
  }
@@ -55,7 +56,7 @@ export class ArrayValidator<T> extends AbstractValidator<T[]> {
55
56
  this.uniqueItems = uniqueItems
56
57
  }
57
58
 
58
- validate(value: unknown, options: ValidationOptions): T[] {
59
+ validate(value: unknown, options?: ValidationOptions): T[] {
59
60
  assertValidation(Array.isArray(value), 'Value is not an "array"')
60
61
 
61
62
  assertValidation(value.length >= this.minItems,
@@ -88,7 +89,7 @@ export class ArrayValidator<T> extends AbstractValidator<T[]> {
88
89
 
89
90
  /* -------------------------------------------------------------------------- */
90
91
 
91
- export function _array<V extends Validation>(constraints: ArrayConstraints<V>): ArrayValidator<InferValidation<V>> {
92
+ function _array<V extends Validation>(constraints: ArrayConstraints<V>): ArrayValidator<InferValidation<V>> {
92
93
  const items = constraints.items ? getValidator(constraints.items) : any
93
94
  return new ArrayValidator({ ...constraints, items })
94
95
  }
@@ -1,5 +1,5 @@
1
- import { AbstractValidator, makeValidatorFactory } from '../types'
2
1
  import { assertValidation } from '../errors'
2
+ import { AbstractValidator, makeValidatorFactory } from '../types'
3
3
 
4
4
  /** Constraints to validate a `boolean` with. */
5
5
  export interface BooleanConstraints {
@@ -37,7 +37,7 @@ export class BooleanValidator extends AbstractValidator<boolean> {
37
37
  }
38
38
  }
39
39
 
40
- export function _boolean(constraints: BooleanConstraints): BooleanValidator {
40
+ function _boolean(constraints: BooleanConstraints): BooleanValidator {
41
41
  return new BooleanValidator(constraints)
42
42
  }
43
43
 
@@ -1,5 +1,7 @@
1
- import { AbstractValidator, Validator } from '../types'
2
1
  import { assertValidation } from '../errors'
2
+ import { AbstractValidator } from '../types'
3
+
4
+ import type { Validator } from '../types'
3
5
 
4
6
  /** A `Validator` for _constants_. */
5
7
  export class ConstantValidator<T extends string | number | boolean | null> extends AbstractValidator<T> {
@@ -64,7 +64,7 @@ export class DateValidator extends AbstractValidator<Date> {
64
64
  }
65
65
  }
66
66
 
67
- export function _date(constraints: DateConstraints): DateValidator {
67
+ function _date(constraints: DateConstraints): DateValidator {
68
68
  return new DateValidator(constraints)
69
69
  }
70
70
 
@@ -1,11 +1,13 @@
1
1
  import { ValidationError } from '../errors'
2
- import { ValidationOptions, AbstractValidator } from '../types'
2
+ import { AbstractValidator, defaultValidationOptions } from '../types'
3
+
4
+ import type { ValidationOptions } from '../types'
3
5
 
4
6
  /** A `Validator` validating _nothing_. */
5
7
  export class NeverValidator extends AbstractValidator<never> {
6
8
  optional: true = true
7
9
 
8
- validate(value: unknown, options: ValidationOptions): never {
10
+ validate(value: unknown, options: ValidationOptions = defaultValidationOptions): never {
9
11
  const { stripForbiddenProperties } = options
10
12
 
11
13
  // @ts-expect-error
@@ -1,6 +1,7 @@
1
- import { Branding, Validator, AbstractValidator, makeValidatorFactory } from '../types'
2
- import { assertSchema, assertValidation } from '../errors'
3
- import { ValidationError } from '..'
1
+ import { assertSchema, assertValidation, ValidationError } from '../errors'
2
+ import { AbstractValidator, makeValidatorFactory } from '../types'
3
+
4
+ import type { Branding, Validator } from '../types'
4
5
 
5
6
  /* ========================================================================== */
6
7
 
@@ -160,10 +161,10 @@ export class NumberValidator<N extends number = number> extends AbstractValidato
160
161
  }
161
162
 
162
163
 
163
- export function _number(constraints: NumberConstraints): NumberValidator<number>
164
- export function _number<N extends number>(constraints: NumberConstraints): NumberValidator<N>
165
- export function _number<B extends string>(constraints: BrandedNumberConstraints<B>): NumberValidator<number & Branding<B>>
166
- export function _number(constraints: NumberConstraints): Validator<number> {
164
+ function _number(constraints: NumberConstraints): NumberValidator<number>
165
+ function _number<N extends number>(constraints: NumberConstraints): NumberValidator<N>
166
+ function _number<B extends string>(constraints: BrandedNumberConstraints<B>): NumberValidator<number & Branding<B>>
167
+ function _number(constraints: NumberConstraints): Validator<number> {
167
168
  return new NumberValidator(constraints)
168
169
  }
169
170
 
@@ -1,20 +1,20 @@
1
+ import { assertValidation, ValidationErrorBuilder } from '../errors'
2
+ // eslint-disable-next-line import/no-cycle
3
+ import { allowAdditionalProperties } from '../schema'
1
4
  import {
2
- InferSchema,
3
- Schema,
4
- TupleRestParameter,
5
- ValidationOptions,
6
- Validator,
7
5
  AbstractValidator,
8
6
  additionalValidator,
7
+ defaultValidationOptions,
8
+ makeValidatorFactory,
9
9
  restValidator,
10
10
  schemaValidator,
11
- makeValidatorFactory,
12
- Validation,
13
- InferValidation,
14
11
  } from '../types'
15
- import { assertValidation, ValidationErrorBuilder } from '../errors'
16
12
  import { getValidator } from '../utilities'
17
- import { allowAdditionalProperties } from '../schema'
13
+
14
+ import type {
15
+ InferSchema, InferValidation, Schema, TupleRestParameter, Validation, ValidationOptions,
16
+ Validator,
17
+ } from '../types'
18
18
 
19
19
  /* ========================================================================== *
20
20
  * OBJECT VALIDATOR *
@@ -49,7 +49,7 @@ export class ObjectValidator<S extends Schema> extends AbstractValidator<InferSc
49
49
  this.schema = schema
50
50
  }
51
51
 
52
- validate(value: unknown, options: ValidationOptions): InferSchema<S> {
52
+ validate(value: unknown, options: ValidationOptions = defaultValidationOptions): InferSchema<S> {
53
53
  assertValidation(typeof value === 'object', 'Value is not an "object"')
54
54
  assertValidation(value !== null, 'Value is "null"')
55
55
 
@@ -120,7 +120,7 @@ export class ObjectValidator<S extends Schema> extends AbstractValidator<InferSc
120
120
  }
121
121
  }
122
122
 
123
- export function _object<S extends Schema>(schema: S): S & {
123
+ function _object<S extends Schema>(schema: S): S & {
124
124
  [Symbol.iterator](): Generator<TupleRestParameter<InferSchema<S>>>
125
125
  } {
126
126
  const validator = new ObjectValidator(schema)
@@ -1,6 +1,8 @@
1
- import { AbstractValidator, InferValidation, Validation, ValidationOptions, Validator } from '../types'
1
+ import { AbstractValidator, defaultValidationOptions } from '../types'
2
2
  import { getValidator } from '../utilities'
3
3
 
4
+ import type { InferValidation, Validation, ValidationOptions, Validator } from '../types'
5
+
4
6
  /**
5
7
  * A `Validator` for _optional_ properties (that is `type | undefined`).
6
8
  */
@@ -10,7 +12,6 @@ export class OptionalValidator<
10
12
  > extends AbstractValidator<D extends undefined ? T | undefined : T> {
11
13
  validator: Validator<T>
12
14
  defaultValue: T | undefined
13
- optional: D extends undefined ? true : false
14
15
 
15
16
  constructor(validator: Validator<T>)
16
17
  constructor(validator: Validator<T>, defaultValue: D)
@@ -25,17 +26,13 @@ export class OptionalValidator<
25
26
  }
26
27
 
27
28
  try {
28
- this.defaultValue = validator.validate(defaultValue, {
29
- stripAdditionalProperties: false,
30
- stripForbiddenProperties: false,
31
- stripOptionalNulls: false,
32
- })
29
+ this.defaultValue = validator.validate(defaultValue, defaultValidationOptions)
33
30
  } catch (cause) {
34
31
  throw new TypeError('Default value does not match validator', { cause })
35
32
  }
36
33
  }
37
34
 
38
- validate(value: unknown, options: ValidationOptions): D extends undefined ? T | undefined : T {
35
+ validate(value: unknown, options?: ValidationOptions): D extends undefined ? T | undefined : T {
39
36
  if (value === undefined) return this.defaultValue as any // do not validate defaults!
40
37
  return this.validator.validate(value, options)
41
38
  }
@@ -1,5 +1,7 @@
1
- import { Branding, Validator, AbstractValidator, makeValidatorFactory } from '../types'
2
- import { assertValidation, assertSchema } from '../errors'
1
+ import { assertSchema, assertValidation } from '../errors'
2
+ import { AbstractValidator, makeValidatorFactory } from '../types'
3
+
4
+ import type { Branding, Validator } from '../types'
3
5
 
4
6
  /** Constraints to validate a `string` with. */
5
7
  export interface StringConstraints {
@@ -68,10 +70,10 @@ export class StringValidator<S extends string = string> extends AbstractValidato
68
70
  }
69
71
  }
70
72
 
71
- export function _string(constraints: StringConstraints): StringValidator<string>
72
- export function _string<S extends string>(constraints: StringConstraints): StringValidator<S>
73
- export function _string<B extends string>(constraints: BrandedStringConstraints<B>): StringValidator<string & Branding<B>>
74
- export function _string(constraints: StringConstraints): Validator<string> {
73
+ function _string(constraints: StringConstraints): StringValidator<string>
74
+ function _string<S extends string>(constraints: StringConstraints): StringValidator<S>
75
+ function _string<B extends string>(constraints: BrandedStringConstraints<B>): StringValidator<string & Branding<B>>
76
+ function _string(constraints: StringConstraints): Validator<string> {
75
77
  return new StringValidator(constraints)
76
78
  }
77
79
 
@@ -1,9 +1,11 @@
1
- import { Tuple, InferTuple, Validator, AbstractValidator, restValidator } from '../types'
2
- import { ValidationOptions } from '../types'
3
1
  import { assertValidation, ValidationError } from '../errors'
2
+ import { AbstractValidator, restValidator } from '../types'
3
+ // eslint-disable-next-line import/no-cycle
4
4
  import { getValidator } from '../utilities'
5
5
  import { nullValidator } from './constant'
6
6
 
7
+ import type { InferTuple, Tuple, ValidationOptions, Validator } from '../types'
8
+
7
9
  export interface TupleMember { single: boolean, validator: Validator }
8
10
 
9
11
  /** A `Validator` for _tuples_. */
@@ -29,7 +31,7 @@ export class TupleValidator<T extends Tuple> extends AbstractValidator<InferTupl
29
31
  this.tuple = tuple
30
32
  }
31
33
 
32
- validate(value: unknown, options: ValidationOptions): InferTuple<T> {
34
+ validate(value: unknown, options?: ValidationOptions): InferTuple<T> {
33
35
  assertValidation(Array.isArray(value), 'Value is not an "array"')
34
36
 
35
37
  // Empty tuples
@@ -1,11 +1,13 @@
1
1
  import { assertSchema, ValidationErrorBuilder } from '../errors'
2
- import {
3
- AbstractValidator, InferValidation,
2
+ import { AbstractValidator } from '../types'
3
+ import { getValidator } from '../utilities'
4
+
5
+ import type {
6
+ InferValidation,
4
7
  Validation,
5
8
  ValidationOptions,
6
9
  Validator,
7
10
  } from '../types'
8
- import { getValidator } from '../utilities'
9
11
 
10
12
  /* -------------------------------------------------------------------------- */
11
13
 
@@ -34,7 +36,7 @@ export class OneOfValidator<A extends OneOfArguments> extends AbstractValidator<
34
36
  assertSchema(this.validators.length > 0, 'At least one validation required in "oneOf"')
35
37
  }
36
38
 
37
- validate(value: unknown, options: ValidationOptions): InferOneOfValidationType<A> {
39
+ validate(value: unknown, options?: ValidationOptions): InferOneOfValidationType<A> {
38
40
  const builder = new ValidationErrorBuilder()
39
41
  for (const validator of this.validators) {
40
42
  try {
@@ -75,7 +77,7 @@ export class AllOfValidator<A extends AllOfArguments> extends AbstractValidator<
75
77
  assertSchema(this.validators.length > 0, 'At least one validation required in "allOf"')
76
78
  }
77
79
 
78
- validate(value: unknown, options: ValidationOptions): InferAllOfValidationType<A> {
80
+ validate(value: unknown, options?: ValidationOptions): InferAllOfValidationType<A> {
79
81
  for (const validator of this.validators) {
80
82
  value = validator.validate(value, options)
81
83
  }
@@ -1,7 +1,10 @@
1
- import { ConstantValidator } from './constant'
2
1
  import { ValidationError, ValidationErrorBuilder } from '../errors'
3
- import { Schema, Validator, AbstractValidator, makeValidatorFactory } from '../types'
4
- import { ObjectValidator, ValidationOptions } from '..'
2
+ import { AbstractValidator, makeValidatorFactory } from '../types'
3
+ import { ConstantValidator } from './constant'
4
+ import { ObjectValidator } from './object'
5
+
6
+ import type { ValidationOptions } from '..'
7
+ import type { Schema, Validator } from '../types'
5
8
 
6
9
  const KEYS: Exclude<keyof URLConstraints, 'searchParams'>[] = [
7
10
  'href',
@@ -127,11 +130,9 @@ export class URLValidator extends AbstractValidator<URL> {
127
130
  }
128
131
  }
129
132
 
130
- const anyURLValidator = new URLValidator()
131
-
132
- export function _url(constraints: URLConstraints): URLValidator {
133
+ function _url(constraints: URLConstraints): URLValidator {
133
134
  return new URLValidator(constraints)
134
135
  }
135
136
 
136
137
  /** Validate URLs and convert them to `URL` instances. */
137
- export const url = makeValidatorFactory(anyURLValidator, _url)
138
+ export const url = makeValidatorFactory(new URLValidator(), _url)