justus 0.5.1 → 0.5.3

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/src/types.ts CHANGED
@@ -52,7 +52,17 @@ export const defaultValidationOptions: Readonly<Required<ValidationOptions>> = {
52
52
  export interface Validator<T = any, I = T> extends Iterable<TupleRestParameter<T, I>> {
53
53
  [Symbol.justusValidator]: this
54
54
 
55
- optional?: boolean
55
+ /**
56
+ * A flag indicating whether the type being validated is _optional_ (the input
57
+ * can be `undefined`) or not (default: `false`).
58
+ */
59
+ optional: boolean
60
+ /**
61
+ * The _default_ replaced by this `Validator` when the input is `undefined`.
62
+ *
63
+ * This is used in conjunction with the `optional` flag.
64
+ */
65
+ defaultValue: T | undefined
56
66
 
57
67
  /** Validate a _value_ and optionally convert it to the required `Type` */
58
68
  validate(value: unknown, options?: ValidationOptions | undefined): T
@@ -73,6 +83,7 @@ export function makeValidatorFactory<
73
83
  >(validator: V, factory: F): F & V {
74
84
  return Object.assign(factory, {
75
85
  optional: validator.optional,
86
+ defaultValue: validator.defaultValue,
76
87
  validate: validator.validate.bind(validator),
77
88
  [Symbol.iterator]: validator[Symbol.iterator].bind(validator),
78
89
  [Symbol.justusValidator]: validator,
@@ -87,7 +98,8 @@ export abstract class AbstractValidator<T, I = T>
87
98
  implements Validator<T, I>, Iterable<TupleRestParameter<T, I>> {
88
99
  [Symbol.justusValidator] = this
89
100
 
90
- optional?: boolean = undefined
101
+ optional: boolean = false
102
+ defaultValue: T | undefined = undefined
91
103
 
92
104
  /** Validate a _value_ and optionally convert it to the required `Type` */
93
105
  abstract validate(value: unknown, options?: ValidationOptions | undefined): T
@@ -266,12 +278,12 @@ export type InferSchema<S> =
266
278
  export type InferSchema2<S> = {
267
279
  // this first part of the type infers all keys from the schema into their
268
280
  // type, but makes *each* key optional... we'll restrict in the next part...
269
- [ key in keyof S as key extends string ? key : never ] ? : InferValidation<S[key]>
281
+ -readonly [ key in keyof S as key extends string ? key : never ] ? : InferValidation<S[key]>
270
282
  } & {
271
283
  // this second part infers *only* keys that _do not_ contain a "undefined"
272
284
  // in their unions, and associates them with the inferred value, basically
273
285
  // making the key *non optional*
274
- [ key in keyof S as
286
+ -readonly [ key in keyof S as
275
287
  key extends string ?
276
288
  undefined extends InferValidation<S[key]> ?
277
289
  never :
@@ -290,12 +302,12 @@ export type InferInputSchema<S> =
290
302
  export type InferInputSchema2<S> = {
291
303
  // this first part of the type infers all keys from the schema into their
292
304
  // type, but makes *each* key optional... we'll restrict in the next part...
293
- [ key in keyof S as key extends string ? key : never ] ? : InferInput<S[key]>
305
+ -readonly [ key in keyof S as key extends string ? key : never ] ? : InferInput<S[key]>
294
306
  } & {
295
307
  // this second part infers *only* keys that _do not_ contain a "undefined"
296
308
  // in their unions, and associates them with the inferred value, basically
297
309
  // making the key *non optional*
298
- [ key in keyof S as
310
+ -readonly [ key in keyof S as
299
311
  key extends string ?
300
312
  InferInput<S[key]> extends never ?
301
313
  never :
package/src/utilities.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { assertSchema } from './errors'
1
2
  import { registry } from './registry'
2
3
 
3
4
  import type { Schema, Validation, Validator } from './types'
@@ -6,12 +7,10 @@ import type { Schema, Validation, Validator } from './types'
6
7
  * UTILITY FUNCTIONS *
7
8
  * ========================================================================== */
8
9
 
9
- /**
10
- * Return the `Validator` for the given `Validation`.
11
- *
12
- * When `validation` is `undefined` it will return a `Validator<any>`,
13
- */
10
+ /** Return the `Validator` for the given `Validation` */
14
11
  export function getValidator(validation: Validation): Validator {
12
+ assertSchema(validation !== undefined, 'No validator for undefined validation')
13
+
15
14
  // Null is a constant
16
15
  if (validation === null) return new (registry.get('constant'))(null)
17
16