@dereekb/model 12.0.6 → 12.1.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.
package/index.cjs.js CHANGED
@@ -3584,7 +3584,7 @@ $({ target: 'Object', stat: true, arity: 2, forced: Object.assign !== assign },
3584
3584
  });
3585
3585
 
3586
3586
  function transformAndValidateObject(config) {
3587
- const transformToResult = transformAndValidateObjectResult(config.classType, config.fn, config.optionsForContext);
3587
+ const transformToResult = transformAndValidateObjectResult(config);
3588
3588
  const {
3589
3589
  handleValidationError
3590
3590
  } = config;
@@ -3611,14 +3611,16 @@ function transformAndValidateObject(config) {
3611
3611
  function transformAndValidateObjectFactory(defaults) {
3612
3612
  const {
3613
3613
  handleValidationError: defaultHandleValidationError,
3614
- optionsForContext
3614
+ optionsForContext,
3615
+ defaultValidationOptions
3615
3616
  } = defaults;
3616
3617
  return (classType, fn, handleValidationError) => {
3617
3618
  const config = {
3618
3619
  classType,
3619
3620
  fn,
3620
3621
  handleValidationError: handleValidationError != null ? handleValidationError : defaultHandleValidationError,
3621
- optionsForContext
3622
+ optionsForContext,
3623
+ defaultValidationOptions
3622
3624
  };
3623
3625
  return transformAndValidateObject(config);
3624
3626
  };
@@ -3630,7 +3632,13 @@ function transformAndValidateObjectFactory(defaults) {
3630
3632
  * @param fn
3631
3633
  * @returns
3632
3634
  */
3633
- function transformAndValidateObjectResult(classType, fn, inputOptionsForContext) {
3635
+ function transformAndValidateObjectResult(config) {
3636
+ const {
3637
+ defaultValidationOptions,
3638
+ classType,
3639
+ fn,
3640
+ optionsForContext: inputOptionsForContext
3641
+ } = config;
3634
3642
  const optionsForContext = inputOptionsForContext != null ? inputOptionsForContext : () => ({});
3635
3643
  return async (input, context) => {
3636
3644
  const {
@@ -3641,7 +3649,9 @@ function transformAndValidateObjectResult(classType, fn, inputOptionsForContext)
3641
3649
  // Note: Each variable on the target class must be marked with the @Expose() annotation.
3642
3650
  excludeExtraneousValues: true
3643
3651
  }));
3644
- const validationErrors = await classValidator.validate(object, validateOptions);
3652
+ const validationErrors = await classValidator.validate(object, Object.assign({
3653
+ forbidUnknownValues: false
3654
+ }, defaultValidationOptions, validateOptions));
3645
3655
  if (validationErrors.length) {
3646
3656
  return {
3647
3657
  object,
package/index.esm.js CHANGED
@@ -3582,7 +3582,7 @@ $({ target: 'Object', stat: true, arity: 2, forced: Object.assign !== assign },
3582
3582
  });
3583
3583
 
3584
3584
  function transformAndValidateObject(config) {
3585
- const transformToResult = transformAndValidateObjectResult(config.classType, config.fn, config.optionsForContext);
3585
+ const transformToResult = transformAndValidateObjectResult(config);
3586
3586
  const {
3587
3587
  handleValidationError
3588
3588
  } = config;
@@ -3609,14 +3609,16 @@ function transformAndValidateObject(config) {
3609
3609
  function transformAndValidateObjectFactory(defaults) {
3610
3610
  const {
3611
3611
  handleValidationError: defaultHandleValidationError,
3612
- optionsForContext
3612
+ optionsForContext,
3613
+ defaultValidationOptions
3613
3614
  } = defaults;
3614
3615
  return (classType, fn, handleValidationError) => {
3615
3616
  const config = {
3616
3617
  classType,
3617
3618
  fn,
3618
3619
  handleValidationError: handleValidationError != null ? handleValidationError : defaultHandleValidationError,
3619
- optionsForContext
3620
+ optionsForContext,
3621
+ defaultValidationOptions
3620
3622
  };
3621
3623
  return transformAndValidateObject(config);
3622
3624
  };
@@ -3628,7 +3630,13 @@ function transformAndValidateObjectFactory(defaults) {
3628
3630
  * @param fn
3629
3631
  * @returns
3630
3632
  */
3631
- function transformAndValidateObjectResult(classType, fn, inputOptionsForContext) {
3633
+ function transformAndValidateObjectResult(config) {
3634
+ const {
3635
+ defaultValidationOptions,
3636
+ classType,
3637
+ fn,
3638
+ optionsForContext: inputOptionsForContext
3639
+ } = config;
3632
3640
  const optionsForContext = inputOptionsForContext != null ? inputOptionsForContext : () => ({});
3633
3641
  return async (input, context) => {
3634
3642
  const {
@@ -3639,7 +3647,9 @@ function transformAndValidateObjectResult(classType, fn, inputOptionsForContext)
3639
3647
  // Note: Each variable on the target class must be marked with the @Expose() annotation.
3640
3648
  excludeExtraneousValues: true
3641
3649
  }));
3642
- const validationErrors = await validate(object, validateOptions);
3650
+ const validationErrors = await validate(object, Object.assign({
3651
+ forbidUnknownValues: false
3652
+ }, defaultValidationOptions, validateOptions));
3643
3653
  if (validationErrors.length) {
3644
3654
  return {
3645
3655
  object,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dereekb/model",
3
- "version": "12.0.6",
3
+ "version": "12.1.0",
4
4
  "exports": {
5
5
  ".": {
6
6
  "types": "./src/index.d.ts",
@@ -1,28 +1,24 @@
1
- import { type ClassType } from '@dereekb/util';
1
+ import { Maybe, type ClassType } from '@dereekb/util';
2
2
  import { type ClassTransformOptions } from 'class-transformer';
3
- import { type ValidationError, type ValidationOptions } from 'class-validator';
3
+ import { type ValidationError, type ValidatorOptions } from 'class-validator';
4
4
  export interface TransformAndValidateObjectOutput<T, O> {
5
- object: T;
6
- result: O;
5
+ readonly object: T;
6
+ readonly result: O;
7
7
  }
8
8
  export type TransformAndValidateObjectFunction<T, O, I extends object = object, C = unknown> = (input: I, context?: C) => Promise<TransformAndValidateObjectOutput<T, O>>;
9
9
  export type TransformAndValidateObjectHandleValidate<O = unknown> = (validationErrors: ValidationError[]) => Promise<O>;
10
10
  /**
11
11
  * transformAndValidateObject() configuration that also provides error handling.
12
12
  */
13
- export interface TransformAndValidateObject<T extends object, O, C = unknown> {
14
- readonly classType: ClassType<T>;
15
- readonly fn: (parsed: T) => Promise<O>;
13
+ export interface TransformAndValidateObject<T extends object, O, C = unknown> extends TransformAndValidateObjectResultFunctionConfig<T, O, C> {
16
14
  readonly handleValidationError: TransformAndValidateObjectHandleValidate<O>;
17
- readonly optionsForContext?: TransformAndValidateObjectResultContextOptionsFunction<C>;
18
15
  }
19
16
  export declare function transformAndValidateObject<T extends object, O, I extends object = object, C = unknown>(config: TransformAndValidateObject<T, O, C>): TransformAndValidateObjectFunction<T, O, I, C>;
20
17
  /**
21
18
  * Configuration for the transformAndValidateObject function from transformAndValidateObjectFactory().
22
19
  */
23
- export interface TransformAndValidateObjectFactoryDefaults<C> {
20
+ export interface TransformAndValidateObjectFactoryDefaults<C> extends Pick<TransformAndValidateObjectResultFunctionConfig<any, any, C>, 'defaultValidationOptions' | 'optionsForContext'> {
24
21
  readonly handleValidationError: TransformAndValidateObjectHandleValidate<unknown>;
25
- readonly optionsForContext?: TransformAndValidateObjectResultContextOptionsFunction<C>;
26
22
  }
27
23
  /**
28
24
  * Factory for generating TransformAndValidateObjectFunction functions.
@@ -37,8 +33,8 @@ export type TransformAndValidateObjectFactory<C = unknown> = <T extends object,
37
33
  export declare function transformAndValidateObjectFactory<C = unknown>(defaults: TransformAndValidateObjectFactoryDefaults<C>): TransformAndValidateObjectFactory<C>;
38
34
  export type TransformAndValidateObjectResultFunction<T, O, I extends object = object, C = unknown> = (input: I, context?: C) => Promise<TransformAndValidateObjectResultOutput<T, O>>;
39
35
  export interface TransformAndValidateObjectResultTransformContextOptions {
40
- transform?: ClassTransformOptions;
41
- validate?: ValidationOptions;
36
+ readonly transform?: ClassTransformOptions;
37
+ readonly validate?: ValidatorOptions;
42
38
  }
43
39
  export type TransformAndValidateObjectResultContextOptionsFunction<C> = (context?: C) => TransformAndValidateObjectResultTransformContextOptions;
44
40
  export type TransformAndValidateObjectResultOutput<T, O> = TransformAndValidateObjectSuccessResultOutput<T, O> | TransformAndValidateObjectErrorResultOutput<T>;
@@ -52,6 +48,12 @@ export interface TransformAndValidateObjectErrorResultOutput<T> {
52
48
  readonly object: T;
53
49
  readonly validationErrors: ValidationError[];
54
50
  }
51
+ export interface TransformAndValidateObjectResultFunctionConfig<T extends object, O, C = unknown, I extends object = object> {
52
+ readonly defaultValidationOptions?: Maybe<ValidatorOptions>;
53
+ readonly classType: ClassType<T>;
54
+ readonly fn: (parsed: T) => Promise<O>;
55
+ readonly optionsForContext?: TransformAndValidateObjectResultContextOptionsFunction<C>;
56
+ }
55
57
  /**
56
58
  * Factory function that wraps the input class type and handler function to first transform the input object to a the given class, and then validate it.
57
59
  *
@@ -59,4 +61,4 @@ export interface TransformAndValidateObjectErrorResultOutput<T> {
59
61
  * @param fn
60
62
  * @returns
61
63
  */
62
- export declare function transformAndValidateObjectResult<T extends object, O, I extends object = object, C = unknown>(classType: ClassType<T>, fn: (parsed: T) => Promise<O>, inputOptionsForContext?: TransformAndValidateObjectResultContextOptionsFunction<C>): TransformAndValidateObjectResultFunction<T, O, I, C>;
64
+ export declare function transformAndValidateObjectResult<T extends object, O, I extends object = object, C = unknown>(config: TransformAndValidateObjectResultFunctionConfig<T, O, C, I>): TransformAndValidateObjectResultFunction<T, O, I, C>;