@trayio/commons 4.17.1 → 4.18.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.
@@ -9,7 +9,7 @@ export interface DynamicSchemaBrand {
9
9
  }
10
10
  export interface DynamicSchemaInterface {
11
11
  fromDynamicObject: (schema: DynamicObject, strict?: O.Option<boolean>) => E.Either<Error, DynamicSchema>;
12
- validate: (schema: DynamicSchema | DynamicObject, value: DynamicObject, strict?: O.Option<boolean>) => E.Either<Error, DynamicObject>;
12
+ validate: (schema: DynamicSchema | DynamicObject, value: DynamicObject, strict?: O.Option<boolean>, coerce?: boolean) => E.Either<Error, DynamicObject>;
13
13
  }
14
14
  export declare const DynamicSchema: DynamicSchemaInterface;
15
15
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"DynamicSchema.d.ts","sourceRoot":"","sources":["../../src/dynamicschema/DynamicSchema.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAClC,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAClC,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAG3B,OAAO,EACN,aAAa,EAEb,MAAM,4BAA4B,CAAC;AAqBpC,QAAA,MAAM,2BAA2B,6EAKhC,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,2BAA2B,CAAC,CAAC;AA4CzE,MAAM,WAAW,kBAAkB;IAClC,QAAQ,CAAC,aAAa,EAAE,OAAO,MAAM,CAAC;CACtC;AAED,MAAM,WAAW,sBAAsB;IACtC,iBAAiB,EAAE,CAClB,MAAM,EAAE,aAAa,EACrB,MAAM,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,KACtB,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;IACpC,QAAQ,EAAE,CACT,MAAM,EAAE,aAAa,GAAG,aAAa,EACrC,KAAK,EAAE,aAAa,EACpB,MAAM,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,KACtB,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;CACpC;AAED,eAAO,MAAM,aAAa,EAAE,sBAkC3B,CAAC"}
1
+ {"version":3,"file":"DynamicSchema.d.ts","sourceRoot":"","sources":["../../src/dynamicschema/DynamicSchema.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAClC,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAClC,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAG3B,OAAO,EACN,aAAa,EAEb,MAAM,4BAA4B,CAAC;AA4BpC,QAAA,MAAM,2BAA2B,6EAKhC,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,2BAA2B,CAAC,CAAC;AA4CzE,MAAM,WAAW,kBAAkB;IAClC,QAAQ,CAAC,aAAa,EAAE,OAAO,MAAM,CAAC;CACtC;AAED,MAAM,WAAW,sBAAsB;IACtC,iBAAiB,EAAE,CAClB,MAAM,EAAE,aAAa,EACrB,MAAM,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,KACtB,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;IACpC,QAAQ,EAAE,CACT,MAAM,EAAE,aAAa,GAAG,aAAa,EACrC,KAAK,EAAE,aAAa,EACpB,MAAM,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,EAC1B,MAAM,CAAC,EAAE,OAAO,KACZ,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;CACpC;AAED,eAAO,MAAM,aAAa,EAAE,sBAwC3B,CAAC"}
@@ -43,6 +43,12 @@ const ajv = new ajv_draft_04_1.default({ strict: false, removeAdditional: 'all'
43
43
  See: https://ajv.js.org/packages/ajv-formats.html#formats
44
44
  */
45
45
  (0, ajv_formats_1.default)(ajv);
46
+ const ajvWithCoercion = new ajv_draft_04_1.default({
47
+ strict: false,
48
+ removeAdditional: 'all',
49
+ coerceTypes: true,
50
+ });
51
+ (0, ajv_formats_1.default)(ajvWithCoercion);
46
52
  const isDynamicSchema = (o) => {
47
53
  try {
48
54
  ajv.compile(o);
@@ -92,11 +98,16 @@ exports.DynamicSchema = {
92
98
  preprocessSchema(copiedSchema, strict);
93
99
  return (0, function_1.pipe)(dynamicSchemaTypeCodec.decode(copiedSchema), E.mapLeft((e) => new Error(`Failed to create DynamicSchema: ${e}`)));
94
100
  },
95
- validate: (schema, value, strict = O.none) => {
101
+ validate: (schema, value, strict = O.none, coerce = false) => {
96
102
  const copiedSchema = (0, deep_copy_ts_1.deepCopy)(schema);
97
103
  preprocessSchema(copiedSchema, strict);
98
104
  const copiedValue = (0, deep_copy_ts_1.deepCopy)(value);
99
- const validateE = (0, Try_1.tryToEither)(() => ajv.compile(copiedSchema));
105
+ const validateE = (0, Try_1.tryToEither)(() => {
106
+ if (coerce) {
107
+ return ajvWithCoercion.compile(copiedSchema);
108
+ }
109
+ return ajv.compile(copiedSchema);
110
+ });
100
111
  return (0, function_1.pipe)(validateE, E.chain((validate) => {
101
112
  if (validate(copiedValue)) {
102
113
  return E.right(copiedValue);
@@ -31,6 +31,7 @@ describe('DynamicSchema', () => {
31
31
  let invalidSchema;
32
32
  let value;
33
33
  let valueWithExtraFields;
34
+ let valueWithCoerceableTypes;
34
35
  let invalidValue;
35
36
  beforeEach(() => {
36
37
  validSchema = {
@@ -90,6 +91,18 @@ describe('DynamicSchema', () => {
90
91
  },
91
92
  ],
92
93
  };
94
+ valueWithCoerceableTypes = {
95
+ name: 'John Doe',
96
+ age: '30',
97
+ races: [
98
+ {
99
+ name: 'monza',
100
+ car: {
101
+ model: 'ferrari',
102
+ },
103
+ },
104
+ ],
105
+ };
93
106
  valueWithExtraFields = {
94
107
  name: 'John Doe',
95
108
  age: 30,
@@ -540,6 +553,16 @@ describe('DynamicSchema', () => {
540
553
  }
541
554
  }
542
555
  });
556
+ it('should return a Right with when value matches the schema but has coerceable types', () => {
557
+ const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
558
+ if (E.isRight(schema)) {
559
+ const result = DynamicSchema_1.DynamicSchema.validate(schema.right, valueWithCoerceableTypes, O.some(false), true);
560
+ expect(E.isRight(result)).toBe(true);
561
+ if (E.isRight(result)) {
562
+ expect(result.right).toEqual(value);
563
+ }
564
+ }
565
+ });
543
566
  it('should return a Left Error when the value does not match the schema', () => {
544
567
  const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
545
568
  if (E.isRight(schema)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/commons",
3
- "version": "4.17.1",
3
+ "version": "4.18.1",
4
4
  "description": "Extensions to the standard/core libraries and basic features",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"