justus 0.1.6 → 0.2.1
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/dist/{dts-generator.js → dts-generator.cjs} +6 -15
- package/dist/dts-generator.cjs.map +6 -0
- package/{dts-generator.d.ts → dist/dts-generator.d.ts} +1 -1
- package/dist/dts-generator.mjs +5 -23
- package/dist/dts-generator.mjs.map +2 -2
- package/dist/errors.cjs +113 -0
- package/dist/errors.cjs.map +6 -0
- package/dist/errors.d.ts +55 -0
- package/dist/errors.mjs +85 -0
- package/dist/errors.mjs.map +6 -0
- package/dist/index.cjs +133 -0
- package/dist/index.cjs.map +6 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.mjs +23 -817
- package/dist/index.mjs.map +3 -3
- package/dist/schema.cjs +43 -0
- package/dist/schema.cjs.map +6 -0
- package/dist/schema.d.ts +16 -0
- package/dist/schema.mjs +17 -0
- package/dist/schema.mjs.map +6 -0
- package/dist/types.cjs +60 -0
- package/dist/types.cjs.map +6 -0
- package/dist/types.d.ts +138 -0
- package/dist/types.mjs +33 -0
- package/dist/types.mjs.map +6 -0
- package/dist/utilities.cjs +52 -0
- package/dist/utilities.cjs.map +6 -0
- package/dist/utilities.d.ts +7 -0
- package/dist/utilities.mjs +27 -0
- package/dist/utilities.mjs.map +6 -0
- package/dist/validators/any.cjs +39 -0
- package/dist/validators/any.cjs.map +6 -0
- package/dist/validators/any.d.ts +7 -0
- package/dist/validators/any.mjs +13 -0
- package/dist/validators/any.mjs.map +6 -0
- package/dist/validators/array.cjs +104 -0
- package/dist/validators/array.cjs.map +6 -0
- package/dist/validators/array.d.ts +32 -0
- package/dist/validators/array.mjs +76 -0
- package/dist/validators/array.mjs.map +6 -0
- package/dist/validators/boolean.cjs +56 -0
- package/dist/validators/boolean.cjs.map +6 -0
- package/dist/validators/boolean.d.ts +21 -0
- package/dist/validators/boolean.mjs +30 -0
- package/dist/validators/boolean.mjs.map +6 -0
- package/dist/validators/constant.cjs +51 -0
- package/dist/validators/constant.cjs.map +6 -0
- package/dist/validators/constant.d.ts +12 -0
- package/dist/validators/constant.mjs +24 -0
- package/dist/validators/constant.mjs.map +6 -0
- package/dist/validators/date.cjs +80 -0
- package/dist/validators/date.cjs.map +6 -0
- package/dist/validators/date.d.ts +22 -0
- package/dist/validators/date.mjs +54 -0
- package/dist/validators/date.mjs.map +6 -0
- package/dist/validators/never.cjs +44 -0
- package/dist/validators/never.cjs.map +6 -0
- package/dist/validators/never.d.ts +9 -0
- package/dist/validators/never.mjs +18 -0
- package/dist/validators/never.mjs.map +6 -0
- package/dist/validators/number.cjs +153 -0
- package/dist/validators/number.cjs.map +6 -0
- package/dist/validators/number.d.ts +48 -0
- package/dist/validators/number.mjs +126 -0
- package/dist/validators/number.mjs.map +6 -0
- package/dist/validators/object.cjs +129 -0
- package/dist/validators/object.cjs.map +6 -0
- package/dist/validators/object.d.ts +22 -0
- package/dist/validators/object.mjs +105 -0
- package/dist/validators/object.mjs.map +6 -0
- package/dist/validators/optional.cjs +61 -0
- package/dist/validators/optional.cjs.map +6 -0
- package/dist/validators/optional.d.ts +21 -0
- package/dist/validators/optional.mjs +35 -0
- package/dist/validators/optional.mjs.map +6 -0
- package/dist/validators/string.cjs +84 -0
- package/dist/validators/string.cjs.map +6 -0
- package/dist/validators/string.d.ts +35 -0
- package/dist/validators/string.mjs +57 -0
- package/dist/validators/string.mjs.map +6 -0
- package/dist/validators/tuple.cjs +95 -0
- package/dist/validators/tuple.cjs.map +6 -0
- package/dist/validators/tuple.d.ts +15 -0
- package/dist/validators/tuple.mjs +69 -0
- package/dist/validators/tuple.mjs.map +6 -0
- package/dist/validators/union.cjs +78 -0
- package/dist/validators/union.cjs.map +6 -0
- package/dist/validators/union.d.ts +22 -0
- package/dist/validators/union.mjs +50 -0
- package/dist/validators/union.mjs.map +6 -0
- package/dist/validators/url.cjs +117 -0
- package/dist/validators/url.cjs.map +6 -0
- package/dist/validators/url.d.ts +57 -0
- package/dist/validators/url.mjs +91 -0
- package/dist/validators/url.mjs.map +6 -0
- package/package.json +33 -23
- package/src/dts-generator.ts +6 -21
- package/src/index.ts +23 -23
- package/src/schema.ts +11 -9
- package/src/types.ts +42 -26
- package/src/utilities.ts +9 -6
- package/src/validators/array.ts +6 -5
- package/src/validators/boolean.ts +2 -2
- package/src/validators/constant.ts +3 -1
- package/src/validators/date.ts +1 -1
- package/src/validators/never.ts +4 -2
- package/src/validators/number.ts +8 -7
- package/src/validators/object.ts +15 -18
- package/src/validators/optional.ts +5 -7
- package/src/validators/string.ts +8 -6
- package/src/validators/tuple.ts +7 -5
- package/src/validators/union.ts +7 -5
- package/src/validators/url.ts +8 -7
- package/dist/dts-generator.js.map +0 -6
- package/dist/index.js +0 -948
- package/dist/index.js.map +0 -6
- package/index.d.ts +0 -618
package/src/index.ts
CHANGED
|
@@ -9,31 +9,37 @@ export * from './types'
|
|
|
9
9
|
export * from './utilities'
|
|
10
10
|
|
|
11
11
|
// Validators
|
|
12
|
-
export { allOf, oneOf, AllOfArguments, AllOfValidator, InferAllOfValidationType, InferOneOfValidationType, OneOfArguments, OneOfValidator } from './validators/union'
|
|
13
12
|
export { any, AnyValidator } from './validators/any'
|
|
14
|
-
export {
|
|
15
|
-
export {
|
|
13
|
+
export { array, arrayOf, ArrayValidator, AnyArrayValidator } from './validators/array'
|
|
14
|
+
export { boolean, BooleanValidator } from './validators/boolean'
|
|
16
15
|
export { constant, ConstantValidator } from './validators/constant'
|
|
17
|
-
export {
|
|
16
|
+
export { date, DateValidator } from './validators/date'
|
|
18
17
|
export { never, NeverValidator } from './validators/never'
|
|
19
|
-
export {
|
|
20
|
-
export {
|
|
18
|
+
export { AnyNumberValidator, number, NumberValidator } from './validators/number'
|
|
19
|
+
export { AnyObjectValidator, object, objectOf, ObjectValidator } from './validators/object'
|
|
21
20
|
export { optional, OptionalValidator } from './validators/optional'
|
|
22
|
-
export {
|
|
23
|
-
export { tuple,
|
|
24
|
-
export {
|
|
21
|
+
export { AnyStringValidator, string, StringValidator } from './validators/string'
|
|
22
|
+
export { tuple, TupleValidator } from './validators/tuple'
|
|
23
|
+
export { allOf, AllOfValidator, oneOf, OneOfValidator } from './validators/union'
|
|
24
|
+
export { url, URLValidator } from './validators/url'
|
|
25
|
+
|
|
26
|
+
// Validator Types
|
|
27
|
+
export type { ArrayConstraints } from './validators/array'
|
|
28
|
+
export type { BooleanConstraints } from './validators/boolean'
|
|
29
|
+
export type { DateConstraints } from './validators/date'
|
|
30
|
+
export type { BrandedNumberConstraints } from './validators/number'
|
|
31
|
+
export type { BrandedStringConstraints, StringConstraints } from './validators/string'
|
|
32
|
+
export type { TupleMember } from './validators/tuple'
|
|
33
|
+
export type { URLConstraints } from './validators/url'
|
|
25
34
|
|
|
26
35
|
/* ========================================================================== *
|
|
27
36
|
* VALIDATE FUNCTION (our main entry point) *
|
|
28
37
|
* ========================================================================== */
|
|
29
38
|
|
|
30
|
-
import
|
|
39
|
+
import { defaultValidationOptions } from './types'
|
|
31
40
|
import { getValidator } from './utilities'
|
|
32
41
|
|
|
33
|
-
|
|
34
|
-
export type ValidateOptions = {
|
|
35
|
-
-readonly [ key in keyof ValidationOptions ]?: ValidationOptions[key] | undefined
|
|
36
|
-
}
|
|
42
|
+
import type { InferValidation, Validation, ValidationOptions } from './types'
|
|
37
43
|
|
|
38
44
|
/**
|
|
39
45
|
* Validate a _value_ using the specified `Validation`.
|
|
@@ -44,15 +50,9 @@ export type ValidateOptions = {
|
|
|
44
50
|
export function validate<V extends Validation>(
|
|
45
51
|
validation: V,
|
|
46
52
|
value: any,
|
|
47
|
-
options
|
|
53
|
+
options?: ValidationOptions,
|
|
48
54
|
): InferValidation<V> {
|
|
49
|
-
const opts: ValidationOptions = {
|
|
50
|
-
stripAdditionalProperties: false,
|
|
51
|
-
stripForbiddenProperties: false,
|
|
52
|
-
stripOptionalNulls: false,
|
|
53
|
-
...options,
|
|
54
|
-
}
|
|
55
|
-
|
|
55
|
+
const opts: ValidationOptions = { ...defaultValidationOptions, ...options }
|
|
56
56
|
return getValidator(validation).validate(value, opts)
|
|
57
57
|
}
|
|
58
58
|
|
|
@@ -73,7 +73,7 @@ export function validate<V extends Validation>(
|
|
|
73
73
|
export function strip<V extends Validation>(
|
|
74
74
|
validation: V,
|
|
75
75
|
value: any,
|
|
76
|
-
options
|
|
76
|
+
options?: ValidationOptions,
|
|
77
77
|
): InferValidation<V> {
|
|
78
78
|
const opts: ValidationOptions = {
|
|
79
79
|
stripAdditionalProperties: true,
|
package/src/schema.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
// import { additionalValidator } from './types'
|
|
2
|
+
// eslint-disable-next-line import/no-cycle
|
|
2
3
|
import { getValidator } from './utilities'
|
|
4
|
+
import { any } from './validators/any'
|
|
3
5
|
|
|
4
|
-
import {
|
|
5
|
-
AdditionalProperties,
|
|
6
|
-
InferValidation,
|
|
6
|
+
import type {
|
|
7
|
+
AdditionalProperties, InferValidation,
|
|
7
8
|
Validation,
|
|
8
9
|
Validator,
|
|
9
|
-
additionalValidator,
|
|
10
10
|
} from './types'
|
|
11
11
|
|
|
12
12
|
/* ========================================================================== *
|
|
@@ -20,10 +20,10 @@ export function _allowAdditionalProperties(allow: false): AdditionalProperties<f
|
|
|
20
20
|
export function _allowAdditionalProperties<V extends Validation>(validation: V): AdditionalProperties<Validator<InferValidation<V>>>
|
|
21
21
|
|
|
22
22
|
export function _allowAdditionalProperties(options?: Validation | boolean): AdditionalProperties<Validator | false> {
|
|
23
|
-
if (options === false) return { [
|
|
24
|
-
if (options === true) return { [
|
|
23
|
+
if (options === false) return { [Symbol.justusAdditionalValidator]: false }
|
|
24
|
+
if (options === true) return { [Symbol.justusAdditionalValidator]: any }
|
|
25
25
|
|
|
26
|
-
return { [
|
|
26
|
+
return { [Symbol.justusAdditionalValidator]: options ? getValidator(options) : any }
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
/**
|
|
@@ -38,4 +38,6 @@ export function _allowAdditionalProperties(options?: Validation | boolean): Addi
|
|
|
38
38
|
export const allowAdditionalProperties = _allowAdditionalProperties as
|
|
39
39
|
typeof _allowAdditionalProperties & AdditionalProperties<Validator<any>>
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
// Remember to inject our `any` validator as the default for when
|
|
42
|
+
// `allowAdditionalProperties` is _not_ used as a function
|
|
43
|
+
allowAdditionalProperties[Symbol.justusAdditionalValidator] = any
|
package/src/types.ts
CHANGED
|
@@ -2,17 +2,23 @@
|
|
|
2
2
|
* SYMBOLS IDENTIFYING SPECIAL FUNCTIONALITIES *
|
|
3
3
|
* ========================================================================== */
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
/** A symbol indicating the `Validator` for a `Schema
|
|
12
|
-
|
|
5
|
+
declare global {
|
|
6
|
+
interface SymbolConstructor {
|
|
7
|
+
/** A symbol associated with a `Validator` instance. */
|
|
8
|
+
readonly justusValidator: unique symbol
|
|
9
|
+
/** A symbol indicating the `Validator` for a `Tuple`'s rest parameter. */
|
|
10
|
+
readonly justusRestValidator: unique symbol
|
|
11
|
+
/** A symbol indicating the `Validator` for a `Schema`'s additional properties. */
|
|
12
|
+
readonly justusAdditionalValidator: unique symbol
|
|
13
|
+
}
|
|
14
|
+
}
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
/* Inject our symbols as globals */
|
|
17
|
+
Object.defineProperties(Symbol, {
|
|
18
|
+
justusValidator: { value: Symbol.for('justus.Validator') },
|
|
19
|
+
justusRestValidator: { value: Symbol.for('justus.restValidator') },
|
|
20
|
+
justusAdditionalValidator: { value: Symbol.for('justus.additionalValidator') },
|
|
21
|
+
})
|
|
16
22
|
|
|
17
23
|
/* ========================================================================== *
|
|
18
24
|
* BASIC VALIDATION TYPES *
|
|
@@ -22,12 +28,21 @@ export const additionalValidator = Symbol.for('justus.additionalValidator')
|
|
|
22
28
|
* Options to be using while validating.
|
|
23
29
|
*/
|
|
24
30
|
export interface ValidationOptions {
|
|
25
|
-
/** Strip additional, undeclared properties from objects */
|
|
26
|
-
|
|
27
|
-
/** Strip `null`s from an object when associated with an optional key */
|
|
28
|
-
|
|
29
|
-
/** Ignore and strip forbidden (`never`) properties from objects */
|
|
30
|
-
|
|
31
|
+
/** Strip additional, undeclared properties from objects (default: `false`) */
|
|
32
|
+
stripAdditionalProperties?: boolean,
|
|
33
|
+
/** Strip `null`s from an object when associated with an optional key (default: `false`) */
|
|
34
|
+
stripOptionalNulls?: boolean,
|
|
35
|
+
/** Ignore and strip forbidden (`never`) properties from objects (default: `false`) */
|
|
36
|
+
stripForbiddenProperties?: boolean,
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Default validation options.
|
|
41
|
+
*/
|
|
42
|
+
export const defaultValidationOptions: Readonly<Required<ValidationOptions>> = {
|
|
43
|
+
stripAdditionalProperties: false,
|
|
44
|
+
stripForbiddenProperties: false,
|
|
45
|
+
stripOptionalNulls: false,
|
|
31
46
|
}
|
|
32
47
|
|
|
33
48
|
/**
|
|
@@ -35,12 +50,12 @@ export interface ValidationOptions {
|
|
|
35
50
|
* (possibly) converting it the required type `T`.
|
|
36
51
|
*/
|
|
37
52
|
export interface Validator<T = any> extends Iterable<TupleRestParameter<T>> {
|
|
38
|
-
[
|
|
53
|
+
[Symbol.justusValidator]: this
|
|
39
54
|
|
|
40
55
|
optional?: boolean
|
|
41
56
|
|
|
42
57
|
/** Validate a _value_ and optionally convert it to the required `Type` */
|
|
43
|
-
validate(value: unknown, options
|
|
58
|
+
validate(value: unknown, options?: ValidationOptions | undefined): T
|
|
44
59
|
|
|
45
60
|
/** Allow any `Validator` to be used as a rest parameter in `Tuple`s */
|
|
46
61
|
[Symbol.iterator](): Generator<TupleRestParameter<T>>;
|
|
@@ -60,7 +75,7 @@ export function makeValidatorFactory<
|
|
|
60
75
|
optional: validator.optional,
|
|
61
76
|
validate: validator.validate.bind(validator),
|
|
62
77
|
[Symbol.iterator]: validator[Symbol.iterator].bind(validator),
|
|
63
|
-
[
|
|
78
|
+
[Symbol.justusValidator]: validator,
|
|
64
79
|
}) as F & V
|
|
65
80
|
}
|
|
66
81
|
|
|
@@ -68,17 +83,18 @@ export function makeValidatorFactory<
|
|
|
68
83
|
* A `Validator` is an object capable of validating a given _value_ and
|
|
69
84
|
* (possibly) converting it the required type `T`.
|
|
70
85
|
*/
|
|
71
|
-
export abstract class AbstractValidator<T = any>
|
|
72
|
-
|
|
86
|
+
export abstract class AbstractValidator<T = any>
|
|
87
|
+
implements Validator<T>, Iterable<TupleRestParameter<T>> {
|
|
88
|
+
[Symbol.justusValidator] = this
|
|
73
89
|
|
|
74
90
|
optional?: boolean = undefined
|
|
75
91
|
|
|
76
92
|
/** Validate a _value_ and optionally convert it to the required `Type` */
|
|
77
|
-
abstract validate(value: unknown, options
|
|
93
|
+
abstract validate(value: unknown, options?: ValidationOptions | undefined): T
|
|
78
94
|
|
|
79
95
|
/** Allow any `Validator` to be used as a rest parameter in `Tuple`s */
|
|
80
96
|
* [Symbol.iterator](): Generator<TupleRestParameter<T>> {
|
|
81
|
-
yield { [
|
|
97
|
+
yield { [Symbol.justusRestValidator]: this }
|
|
82
98
|
}
|
|
83
99
|
}
|
|
84
100
|
|
|
@@ -149,7 +165,7 @@ export type Tuple = readonly (Validation | TupleRestParameter)[]
|
|
|
149
165
|
* ```
|
|
150
166
|
*/
|
|
151
167
|
export type TupleRestParameter<T = any> = {
|
|
152
|
-
[
|
|
168
|
+
[Symbol.justusRestValidator] : Validator<T>
|
|
153
169
|
}
|
|
154
170
|
|
|
155
171
|
/**
|
|
@@ -182,7 +198,7 @@ export type InferTuple<T> =
|
|
|
182
198
|
*/
|
|
183
199
|
export interface Schema {
|
|
184
200
|
[ key: string ] : Validation
|
|
185
|
-
[
|
|
201
|
+
[ Symbol.justusAdditionalValidator ]?: Validator | false
|
|
186
202
|
}
|
|
187
203
|
|
|
188
204
|
/**
|
|
@@ -190,7 +206,7 @@ export interface Schema {
|
|
|
190
206
|
* properties, and the `Validator` used to validate them.
|
|
191
207
|
*/
|
|
192
208
|
export interface AdditionalProperties<V extends Validator | false> {
|
|
193
|
-
[
|
|
209
|
+
[ Symbol.justusAdditionalValidator ]: V
|
|
194
210
|
}
|
|
195
211
|
|
|
196
212
|
/* ========================================================================== *
|
package/src/utilities.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { ConstantValidator, nullValidator } from './validators/constant'
|
|
2
|
-
|
|
3
|
-
import { TupleValidator } from './validators/tuple'
|
|
2
|
+
// eslint-disable-next-line import/no-cycle
|
|
4
3
|
import { ObjectValidator } from './validators/object'
|
|
4
|
+
// eslint-disable-next-line import/no-cycle
|
|
5
|
+
import { TupleValidator } from './validators/tuple'
|
|
6
|
+
|
|
7
|
+
import type { Schema, Validation, Validator } from './types'
|
|
5
8
|
|
|
6
9
|
/* ========================================================================== *
|
|
7
10
|
* UTILITY FUNCTIONS *
|
|
@@ -16,8 +19,10 @@ export function getValidator(validation: Validation): Validator {
|
|
|
16
19
|
// Null is a constant
|
|
17
20
|
if (validation === null) return nullValidator
|
|
18
21
|
|
|
19
|
-
//
|
|
20
|
-
if ((<any> validation)[
|
|
22
|
+
// Anything with a validor associated with
|
|
23
|
+
if ((<any> validation)[Symbol.justusValidator]) {
|
|
24
|
+
return (<any> validation)[Symbol.justusValidator] as Validator
|
|
25
|
+
}
|
|
21
26
|
|
|
22
27
|
// Other types
|
|
23
28
|
switch (typeof validation) {
|
|
@@ -29,8 +34,6 @@ export function getValidator(validation: Validation): Validator {
|
|
|
29
34
|
|
|
30
35
|
// other objects...
|
|
31
36
|
case 'object':
|
|
32
|
-
// pre-compiled schema with validator
|
|
33
|
-
if (schemaValidator in validation) return (<any> validation)[schemaValidator]
|
|
34
37
|
// arrays are tuples
|
|
35
38
|
if (Array.isArray(validation)) return new TupleValidator(validation)
|
|
36
39
|
// any other object is a schema
|
package/src/validators/array.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { assertSchema, assertValidation, ValidationErrorBuilder } from '../errors'
|
|
2
|
-
import {
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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> {
|
package/src/validators/date.ts
CHANGED
package/src/validators/never.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { ValidationError } from '../errors'
|
|
2
|
-
import {
|
|
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
|
package/src/validators/number.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
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
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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
|
|
package/src/validators/object.ts
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
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
|
-
|
|
9
|
-
restValidator,
|
|
10
|
-
schemaValidator,
|
|
6
|
+
defaultValidationOptions,
|
|
11
7
|
makeValidatorFactory,
|
|
12
|
-
Validation,
|
|
13
|
-
InferValidation,
|
|
14
8
|
} from '../types'
|
|
15
|
-
import { assertValidation, ValidationErrorBuilder } from '../errors'
|
|
16
9
|
import { getValidator } from '../utilities'
|
|
17
|
-
|
|
10
|
+
|
|
11
|
+
import type {
|
|
12
|
+
InferSchema, InferValidation, Schema, TupleRestParameter, Validation, ValidationOptions,
|
|
13
|
+
Validator,
|
|
14
|
+
} from '../types'
|
|
18
15
|
|
|
19
16
|
/* ========================================================================== *
|
|
20
17
|
* OBJECT VALIDATOR *
|
|
@@ -38,9 +35,9 @@ export class ObjectValidator<S extends Schema> extends AbstractValidator<InferSc
|
|
|
38
35
|
|
|
39
36
|
constructor(schema: S) {
|
|
40
37
|
super()
|
|
41
|
-
const { [
|
|
38
|
+
const { [Symbol.justusAdditionalValidator]: additional, ...properties } = schema
|
|
42
39
|
|
|
43
|
-
if (additional) this.additionalProperties =
|
|
40
|
+
if (additional) this.additionalProperties = additional
|
|
44
41
|
|
|
45
42
|
for (const key of Object.keys(properties)) {
|
|
46
43
|
this.validators.set(key, getValidator(properties[key]))
|
|
@@ -49,7 +46,7 @@ export class ObjectValidator<S extends Schema> extends AbstractValidator<InferSc
|
|
|
49
46
|
this.schema = schema
|
|
50
47
|
}
|
|
51
48
|
|
|
52
|
-
validate(value: unknown, options: ValidationOptions): InferSchema<S> {
|
|
49
|
+
validate(value: unknown, options: ValidationOptions = defaultValidationOptions): InferSchema<S> {
|
|
53
50
|
assertValidation(typeof value === 'object', 'Value is not an "object"')
|
|
54
51
|
assertValidation(value !== null, 'Value is "null"')
|
|
55
52
|
|
|
@@ -120,16 +117,16 @@ export class ObjectValidator<S extends Schema> extends AbstractValidator<InferSc
|
|
|
120
117
|
}
|
|
121
118
|
}
|
|
122
119
|
|
|
123
|
-
|
|
120
|
+
function _object<S extends Schema>(schema: S): S & {
|
|
124
121
|
[Symbol.iterator](): Generator<TupleRestParameter<InferSchema<S>>>
|
|
125
122
|
} {
|
|
126
123
|
const validator = new ObjectValidator(schema)
|
|
127
124
|
function* iterator(): Generator<TupleRestParameter> {
|
|
128
|
-
yield { [
|
|
125
|
+
yield { [Symbol.justusRestValidator]: validator }
|
|
129
126
|
}
|
|
130
127
|
|
|
131
128
|
return Object.defineProperties(schema, {
|
|
132
|
-
[
|
|
129
|
+
[Symbol.justusValidator]: { value: validator, enumerable: false },
|
|
133
130
|
[Symbol.iterator]: { value: iterator, enumerable: false },
|
|
134
131
|
}) as any
|
|
135
132
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { AbstractValidator,
|
|
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
|
*/
|
|
@@ -24,17 +26,13 @@ export class OptionalValidator<
|
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
try {
|
|
27
|
-
this.defaultValue = validator.validate(defaultValue,
|
|
28
|
-
stripAdditionalProperties: false,
|
|
29
|
-
stripForbiddenProperties: false,
|
|
30
|
-
stripOptionalNulls: false,
|
|
31
|
-
})
|
|
29
|
+
this.defaultValue = validator.validate(defaultValue, defaultValidationOptions)
|
|
32
30
|
} catch (cause) {
|
|
33
31
|
throw new TypeError('Default value does not match validator', { cause })
|
|
34
32
|
}
|
|
35
33
|
}
|
|
36
34
|
|
|
37
|
-
validate(value: unknown, options
|
|
35
|
+
validate(value: unknown, options?: ValidationOptions): D extends undefined ? T | undefined : T {
|
|
38
36
|
if (value === undefined) return this.defaultValue as any // do not validate defaults!
|
|
39
37
|
return this.validator.validate(value, options)
|
|
40
38
|
}
|
package/src/validators/string.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
|
package/src/validators/tuple.ts
CHANGED
|
@@ -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 } 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_. */
|
|
@@ -18,8 +20,8 @@ export class TupleValidator<T extends Tuple> extends AbstractValidator<InferTupl
|
|
|
18
20
|
for (const item of tuple) {
|
|
19
21
|
if (item === null) { // god knows why typeof null === "object"
|
|
20
22
|
members.push({ single: true, validator: nullValidator })
|
|
21
|
-
} else if ((typeof item === 'object') && (
|
|
22
|
-
members.push({ single: false, validator: (<any>item)[
|
|
23
|
+
} else if ((typeof item === 'object') && (Symbol.justusRestValidator in item)) {
|
|
24
|
+
members.push({ single: false, validator: (<any>item)[Symbol.justusRestValidator] })
|
|
23
25
|
} else {
|
|
24
26
|
members.push({ single: true, validator: getValidator(item) })
|
|
25
27
|
}
|
|
@@ -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
|
|
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
|
package/src/validators/union.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { assertSchema, ValidationErrorBuilder } from '../errors'
|
|
2
|
-
import {
|
|
3
|
-
|
|
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
|
|
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
|
|
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
|
}
|
package/src/validators/url.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { ConstantValidator } from './constant'
|
|
2
1
|
import { ValidationError, ValidationErrorBuilder } from '../errors'
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
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
|
-
|
|
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(
|
|
138
|
+
export const url = makeValidatorFactory(new URLValidator(), _url)
|