justus 0.5.3 → 0.5.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/dist/dts-generator.cjs +21 -3
- package/dist/dts-generator.cjs.map +1 -1
- package/dist/dts-generator.mjs +21 -3
- package/dist/dts-generator.mjs.map +1 -1
- package/dist/extra/arn.cjs +1 -1
- package/dist/extra/arn.cjs.map +1 -1
- package/dist/extra/arn.mjs +1 -1
- package/dist/extra/arn.mjs.map +1 -1
- package/dist/index.cjs +13 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +18 -6
- package/dist/index.mjs +9 -2
- package/dist/index.mjs.map +1 -1
- package/dist/types.cjs +0 -7
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.ts +7 -9
- package/dist/types.mjs +0 -6
- package/dist/types.mjs.map +1 -1
- package/dist/utilities.cjs +1 -0
- package/dist/utilities.cjs.map +1 -1
- package/dist/utilities.d.ts +2 -2
- package/dist/utilities.mjs +1 -0
- package/dist/utilities.mjs.map +1 -1
- package/dist/validators/array.cjs +6 -6
- package/dist/validators/array.cjs.map +1 -1
- package/dist/validators/array.d.ts +6 -6
- package/dist/validators/array.mjs +4 -4
- package/dist/validators/array.mjs.map +1 -1
- package/dist/validators/bigint.cjs +146 -0
- package/dist/validators/bigint.cjs.map +6 -0
- package/dist/validators/bigint.d.ts +46 -0
- package/dist/validators/bigint.mjs +118 -0
- package/dist/validators/bigint.mjs.map +6 -0
- package/dist/validators/boolean.cjs +5 -5
- package/dist/validators/boolean.cjs.map +1 -1
- package/dist/validators/boolean.d.ts +2 -2
- package/dist/validators/boolean.mjs +4 -4
- package/dist/validators/boolean.mjs.map +1 -1
- package/dist/validators/constant.cjs +2 -1
- package/dist/validators/constant.cjs.map +1 -1
- package/dist/validators/constant.d.ts +2 -2
- package/dist/validators/constant.mjs +2 -1
- package/dist/validators/constant.mjs.map +1 -1
- package/dist/validators/date.cjs +4 -4
- package/dist/validators/date.cjs.map +1 -1
- package/dist/validators/date.d.ts +2 -2
- package/dist/validators/date.mjs +3 -3
- package/dist/validators/date.mjs.map +1 -1
- package/dist/validators/never.cjs +1 -1
- package/dist/validators/never.cjs.map +1 -1
- package/dist/validators/never.mjs +2 -2
- package/dist/validators/never.mjs.map +1 -1
- package/dist/validators/number.cjs +9 -9
- package/dist/validators/number.cjs.map +1 -1
- package/dist/validators/number.d.ts +4 -4
- package/dist/validators/number.mjs +8 -8
- package/dist/validators/number.mjs.map +1 -1
- package/dist/validators/object.cjs +13 -11
- package/dist/validators/object.cjs.map +1 -1
- package/dist/validators/object.d.ts +2 -2
- package/dist/validators/object.mjs +12 -10
- package/dist/validators/object.mjs.map +1 -1
- package/dist/validators/optional.cjs +1 -1
- package/dist/validators/optional.cjs.map +1 -1
- package/dist/validators/optional.mjs +2 -2
- package/dist/validators/optional.mjs.map +1 -1
- package/dist/validators/string.cjs +6 -6
- package/dist/validators/string.cjs.map +1 -1
- package/dist/validators/string.d.ts +4 -4
- package/dist/validators/string.mjs +5 -5
- package/dist/validators/string.mjs.map +1 -1
- package/dist/validators/union.d.ts +2 -2
- package/package.json +3 -5
- package/src/dts-generator.ts +22 -3
- package/src/extra/arn.ts +1 -1
- package/src/index.ts +26 -8
- package/src/types.ts +7 -12
- package/src/utilities.ts +3 -1
- package/src/validators/array.ts +9 -7
- package/src/validators/bigint.ts +147 -0
- package/src/validators/boolean.ts +3 -3
- package/src/validators/constant.ts +4 -3
- package/src/validators/date.ts +2 -2
- package/src/validators/never.ts +2 -2
- package/src/validators/number.ts +10 -10
- package/src/validators/object.ts +11 -10
- package/src/validators/optional.ts +2 -2
- package/src/validators/string.ts +7 -7
- package/src/validators/union.ts +2 -2
package/src/validators/number.ts
CHANGED
|
@@ -46,7 +46,7 @@ export interface BrandedNumberConstraints<B extends string> extends NumberConstr
|
|
|
46
46
|
/** A `Validator` validating any `number`. */
|
|
47
47
|
export class AnyNumberValidator extends AbstractValidator<number> {
|
|
48
48
|
validate(value: unknown): number {
|
|
49
|
-
assertValidation(typeof value
|
|
49
|
+
assertValidation(typeof value === 'number', 'Value is not a "number"')
|
|
50
50
|
assertValidation(! isNaN(value), 'Number is "NaN"')
|
|
51
51
|
return value
|
|
52
52
|
}
|
|
@@ -131,13 +131,13 @@ export class NumberValidator<N extends number = number> extends AbstractValidato
|
|
|
131
131
|
|
|
132
132
|
validate(value: unknown): N {
|
|
133
133
|
// Allow parsing from strings
|
|
134
|
-
if ((typeof value
|
|
134
|
+
if ((typeof value === 'string') && (this.fromString)) {
|
|
135
135
|
const parsed = +`${value}`
|
|
136
136
|
assertValidation(! isNaN(parsed), 'Number can not be parsed from string')
|
|
137
137
|
value = parsed
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
-
assertValidation(typeof value
|
|
140
|
+
assertValidation(typeof value === 'number', 'Value is not a "number"')
|
|
141
141
|
|
|
142
142
|
if (isNaN(value)) {
|
|
143
143
|
assertValidation(this.allowNaN, 'Number is "NaN"')
|
|
@@ -147,10 +147,10 @@ export class NumberValidator<N extends number = number> extends AbstractValidato
|
|
|
147
147
|
assertValidation(value >= this.minimum, `Number is less than ${this.minimum}`)
|
|
148
148
|
assertValidation(value <= this.maximum, `Number is greater than ${this.maximum}`)
|
|
149
149
|
|
|
150
|
-
assertValidation((this.exclusiveMinimum
|
|
150
|
+
assertValidation((this.exclusiveMinimum === undefined) || (value > this.exclusiveMinimum),
|
|
151
151
|
`Number is less than or equal to ${this.exclusiveMinimum}`)
|
|
152
152
|
|
|
153
|
-
assertValidation((this.exclusiveMaximum
|
|
153
|
+
assertValidation((this.exclusiveMaximum === undefined) || (value < this.exclusiveMaximum),
|
|
154
154
|
`Number is greater than or equal to ${this.exclusiveMaximum}`)
|
|
155
155
|
|
|
156
156
|
assertValidation(this.#isMultipleOf ? this.#isMultipleOf(value) : true,
|
|
@@ -160,12 +160,12 @@ export class NumberValidator<N extends number = number> extends AbstractValidato
|
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
|
|
163
|
-
export function
|
|
164
|
-
export function
|
|
165
|
-
export function
|
|
166
|
-
export function
|
|
163
|
+
export function numberValidatorFactory(constraints: NumberConstraints): NumberValidator<number>
|
|
164
|
+
export function numberValidatorFactory<N extends number>(constraints: NumberConstraints): NumberValidator<N>
|
|
165
|
+
export function numberValidatorFactory<B extends string>(constraints: BrandedNumberConstraints<B>): NumberValidator<number & Branding<B>>
|
|
166
|
+
export function numberValidatorFactory(constraints: NumberConstraints): Validator<number> {
|
|
167
167
|
return new NumberValidator(constraints)
|
|
168
168
|
}
|
|
169
169
|
|
|
170
170
|
/** Validate `number`s. */
|
|
171
|
-
export const number = makeValidatorFactory(new AnyNumberValidator(),
|
|
171
|
+
export const number = makeValidatorFactory(new AnyNumberValidator(), numberValidatorFactory)
|
package/src/validators/object.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { assertValidation, ValidationErrorBuilder } from '../errors'
|
|
2
2
|
import { registry } from '../registry'
|
|
3
|
-
import { AbstractValidator,
|
|
3
|
+
import { AbstractValidator, makeValidatorFactory } from '../types'
|
|
4
4
|
import { getValidator } from '../utilities'
|
|
5
5
|
|
|
6
6
|
import type {
|
|
@@ -17,7 +17,7 @@ import type {
|
|
|
17
17
|
/** A `Validator` validating any `object`. */
|
|
18
18
|
export class AnyObjectValidator extends AbstractValidator<Record<string, any>> {
|
|
19
19
|
validate(value: unknown): Record<string, any> {
|
|
20
|
-
assertValidation(typeof value
|
|
20
|
+
assertValidation(typeof value === 'object', 'Value is not an "object"')
|
|
21
21
|
assertValidation(value !== null, 'Value is "null"')
|
|
22
22
|
return value
|
|
23
23
|
}
|
|
@@ -43,11 +43,11 @@ export class ObjectValidator<S extends Schema> extends AbstractValidator<InferSc
|
|
|
43
43
|
this.schema = schema
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
validate(value: unknown, options: ValidationOptions =
|
|
46
|
+
validate(value: unknown, options: ValidationOptions = {}): InferSchema<S> {
|
|
47
47
|
assertValidation(typeof value === 'object', 'Value is not an "object"')
|
|
48
48
|
assertValidation(value !== null, 'Value is "null"')
|
|
49
49
|
|
|
50
|
-
const { stripAdditionalProperties, stripOptionalNulls } = options
|
|
50
|
+
const { stripAdditionalProperties, stripOptionalNulls, partialValidation } = options
|
|
51
51
|
|
|
52
52
|
const record: { [ k in string | number | symbol ]?: unknown } = value
|
|
53
53
|
const builder = new ValidationErrorBuilder()
|
|
@@ -63,19 +63,20 @@ export class ObjectValidator<S extends Schema> extends AbstractValidator<InferSc
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
// if we have no value, then we have few possibilities:
|
|
66
|
+
// - we are performing a partial validation, so we ignore
|
|
66
67
|
// - the (optional) validator provides a valid value
|
|
67
68
|
// - the validator is optional, so we can simply ignore
|
|
68
69
|
// - the validator is not optional, so the property is missing
|
|
69
70
|
if (original === undefined) {
|
|
71
|
+
if (partialValidation) continue
|
|
70
72
|
try {
|
|
71
73
|
// try to validate, the validator _might_ be giving us a value
|
|
72
74
|
const validated = validator.validate(original, options)
|
|
73
75
|
// put the validated value in the clone, unless optional and undefined
|
|
74
|
-
if (! (optional && (validated
|
|
76
|
+
if (! (optional && (validated === undefined))) clone[key] = validated
|
|
75
77
|
} catch (error) {
|
|
76
78
|
if (optional) continue // original was undefined, so we can skip!
|
|
77
79
|
builder.record('Required property missing', key)
|
|
78
|
-
// builder.record(error, key) // double error!
|
|
79
80
|
}
|
|
80
81
|
|
|
81
82
|
continue
|
|
@@ -85,7 +86,7 @@ export class ObjectValidator<S extends Schema> extends AbstractValidator<InferSc
|
|
|
85
86
|
try {
|
|
86
87
|
const validated = validator.validate(original, options)
|
|
87
88
|
// put the validated value in the clone, unless optional and undefined
|
|
88
|
-
if (! (optional && (validated
|
|
89
|
+
if (! (optional && (validated === undefined))) clone[key] = validated
|
|
89
90
|
} catch (error) {
|
|
90
91
|
builder.record(error, key)
|
|
91
92
|
}
|
|
@@ -114,7 +115,7 @@ export class ObjectValidator<S extends Schema> extends AbstractValidator<InferSc
|
|
|
114
115
|
}
|
|
115
116
|
}
|
|
116
117
|
|
|
117
|
-
export function
|
|
118
|
+
export function objectValidatorFactory<S extends Schema>(schema: S): S & {
|
|
118
119
|
[Symbol.iterator](): Generator<TupleRestParameter<InferSchema<S>, InferInputSchema<S>>>
|
|
119
120
|
} {
|
|
120
121
|
const validator = new ObjectValidator(schema)
|
|
@@ -130,11 +131,11 @@ export function objectFactory<S extends Schema>(schema: S): S & {
|
|
|
130
131
|
}
|
|
131
132
|
|
|
132
133
|
/** Validate `object`s. */
|
|
133
|
-
export const object = makeValidatorFactory(new AnyObjectValidator(),
|
|
134
|
+
export const object = makeValidatorFactory(new AnyObjectValidator(), objectValidatorFactory)
|
|
134
135
|
|
|
135
136
|
/** Validate `Object`s containing only the specified elements. */
|
|
136
137
|
export function objectOf<V extends Validation>(validation: V): Validator<Record<string, InferValidation<V>>> {
|
|
137
|
-
return new ObjectValidator({ [Symbol.justusAdditionalValidator]: getValidator(validation) })
|
|
138
|
+
return new ObjectValidator<any>({ [Symbol.justusAdditionalValidator]: getValidator(validation) })
|
|
138
139
|
}
|
|
139
140
|
|
|
140
141
|
// Register our "object" validator
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AbstractValidator
|
|
1
|
+
import { AbstractValidator } from '../types'
|
|
2
2
|
import { getValidator } from '../utilities'
|
|
3
3
|
|
|
4
4
|
import type { InferInput, InferValidation, Validation, ValidationOptions, Validator } from '../types'
|
|
@@ -27,7 +27,7 @@ export class OptionalValidator<
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
try {
|
|
30
|
-
this.defaultValue = validator.validate(defaultValue,
|
|
30
|
+
this.defaultValue = validator.validate(defaultValue, {})
|
|
31
31
|
} catch (cause) {
|
|
32
32
|
throw new TypeError('Default value does not match validator', { cause })
|
|
33
33
|
}
|
package/src/validators/string.ts
CHANGED
|
@@ -22,7 +22,7 @@ export interface BrandedStringConstraints<B extends string> extends StringConstr
|
|
|
22
22
|
/** A `Validator` validating any `string`. */
|
|
23
23
|
export class AnyStringValidator extends AbstractValidator<string> {
|
|
24
24
|
validate(value: unknown): string {
|
|
25
|
-
assertValidation(typeof value
|
|
25
|
+
assertValidation(typeof value === 'string', 'Value is not a "string"')
|
|
26
26
|
return value
|
|
27
27
|
}
|
|
28
28
|
}
|
|
@@ -55,7 +55,7 @@ export class StringValidator<S extends string = string, I = string> extends Abst
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
validate(value: unknown): S {
|
|
58
|
-
assertValidation(typeof value
|
|
58
|
+
assertValidation(typeof value === 'string', 'Value is not a "string"')
|
|
59
59
|
|
|
60
60
|
assertValidation(value.length >= this.minLength,
|
|
61
61
|
`String must have a minimum length of ${this.minLength}`)
|
|
@@ -70,12 +70,12 @@ export class StringValidator<S extends string = string, I = string> extends Abst
|
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
export function
|
|
74
|
-
export function
|
|
75
|
-
export function
|
|
76
|
-
export function
|
|
73
|
+
export function stringValidatorFactory(constraints: StringConstraints): StringValidator<string>
|
|
74
|
+
export function stringValidatorFactory<S extends string>(constraints: StringConstraints): StringValidator<S>
|
|
75
|
+
export function stringValidatorFactory<B extends string>(constraints: BrandedStringConstraints<B>): StringValidator<string & Branding<B>>
|
|
76
|
+
export function stringValidatorFactory(constraints: StringConstraints): Validator<string> {
|
|
77
77
|
return new StringValidator(constraints)
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
/** Validate `string`s. */
|
|
81
|
-
export const string = makeValidatorFactory(new AnyStringValidator(),
|
|
81
|
+
export const string = makeValidatorFactory(new AnyStringValidator(), stringValidatorFactory)
|
package/src/validators/union.ts
CHANGED
|
@@ -31,7 +31,7 @@ export type InferOneOfInput<A extends OneOfArguments> =
|
|
|
31
31
|
A extends readonly [ infer First, ...infer Rest ] ?
|
|
32
32
|
First extends Validation ?
|
|
33
33
|
Rest extends OneOfArguments ?
|
|
34
|
-
InferInput<First> |
|
|
34
|
+
InferInput<First> | InferOneOfInput<Rest> :
|
|
35
35
|
InferInput<First> :
|
|
36
36
|
never :
|
|
37
37
|
A extends readonly (infer Type)[] ?
|
|
@@ -86,7 +86,7 @@ export type InferAllOfInput<A extends AllOfArguments> =
|
|
|
86
86
|
A extends readonly [ infer First, ...infer Rest ] ?
|
|
87
87
|
First extends Validation ?
|
|
88
88
|
Rest extends AllOfArguments ?
|
|
89
|
-
InferInput<First> &
|
|
89
|
+
InferInput<First> & InferAllOfInput<Rest> :
|
|
90
90
|
InferInput<First> :
|
|
91
91
|
never :
|
|
92
92
|
never
|