@trayio/commons 4.6.0 → 4.7.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.
@@ -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, undefined>;
12
+ validate: (schema: DynamicSchema | DynamicObject, value: DynamicObject, strict?: O.Option<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,SAAS,CAAC,CAAC;CAChC;AAED,eAAO,MAAM,aAAa,EAAE,sBA8B3B,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;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,sBA+B3B,CAAC"}
@@ -37,7 +37,7 @@ const deep_copy_ts_1 = require("deep-copy-ts");
37
37
  const DynamicType_1 = require("../dynamictype/DynamicType");
38
38
  const TypeCodec_1 = require("../codec/TypeCodec");
39
39
  const Try_1 = require("../try/Try");
40
- const ajv = new ajv_draft_04_1.default({ strict: false });
40
+ const ajv = new ajv_draft_04_1.default({ strict: false, removeAdditional: 'all' });
41
41
  /*
42
42
  We define additional formats for the JSON schema
43
43
  See: https://ajv.js.org/packages/ajv-formats.html#formats
@@ -95,10 +95,11 @@ exports.DynamicSchema = {
95
95
  validate: (schema, value, strict = O.none) => {
96
96
  const copiedSchema = (0, deep_copy_ts_1.deepCopy)(schema);
97
97
  preprocessSchema(copiedSchema, strict);
98
+ const copiedValue = (0, deep_copy_ts_1.deepCopy)(value);
98
99
  const validateE = (0, Try_1.tryToEither)(() => ajv.compile(copiedSchema));
99
100
  return (0, function_1.pipe)(validateE, E.chain((validate) => {
100
- if (validate(value)) {
101
- return E.right(undefined);
101
+ if (validate(copiedValue)) {
102
+ return E.right(copiedValue);
102
103
  }
103
104
  return E.left(new Error(ajv.errorsText(validate.errors)));
104
105
  }));
@@ -30,6 +30,7 @@ describe('DynamicSchema', () => {
30
30
  let validSchema;
31
31
  let invalidSchema;
32
32
  let value;
33
+ let valueWithExtraFields;
33
34
  beforeEach(() => {
34
35
  validSchema = {
35
36
  type: 'object',
@@ -40,8 +41,27 @@ describe('DynamicSchema', () => {
40
41
  age: {
41
42
  type: 'number',
42
43
  },
44
+ races: {
45
+ type: 'array',
46
+ items: {
47
+ type: 'object',
48
+ properties: {
49
+ name: {
50
+ type: 'string',
51
+ },
52
+ car: {
53
+ type: 'object',
54
+ properties: {
55
+ model: {
56
+ type: 'string',
57
+ },
58
+ },
59
+ },
60
+ },
61
+ },
62
+ },
43
63
  },
44
- required: ['name', 'age'],
64
+ required: ['name', 'age', 'races'],
45
65
  advanced: [], // not part of json schema but the validator should not be strict with extra elements
46
66
  };
47
67
  invalidSchema = {
@@ -59,6 +79,29 @@ describe('DynamicSchema', () => {
59
79
  value = {
60
80
  name: 'John Doe',
61
81
  age: 30,
82
+ races: [
83
+ {
84
+ name: 'monza',
85
+ car: {
86
+ model: 'ferrari',
87
+ },
88
+ },
89
+ ],
90
+ };
91
+ valueWithExtraFields = {
92
+ name: 'John Doe',
93
+ age: 30,
94
+ extra: 'field',
95
+ races: [
96
+ {
97
+ name: 'monza',
98
+ country: 'italy',
99
+ car: {
100
+ model: 'ferrari',
101
+ year: 2017,
102
+ },
103
+ },
104
+ ],
62
105
  };
63
106
  });
64
107
  describe('fromDynamicObject', () => {
@@ -206,8 +249,29 @@ describe('DynamicSchema', () => {
206
249
  age: {
207
250
  type: 'number',
208
251
  },
252
+ races: {
253
+ type: 'array',
254
+ items: {
255
+ type: 'object',
256
+ additionalProperties: false,
257
+ properties: {
258
+ name: {
259
+ type: 'string',
260
+ },
261
+ car: {
262
+ type: 'object',
263
+ additionalProperties: false,
264
+ properties: {
265
+ model: {
266
+ type: 'string',
267
+ },
268
+ },
269
+ },
270
+ },
271
+ },
272
+ },
209
273
  },
210
- required: ['name', 'age'],
274
+ required: ['name', 'age', 'races'],
211
275
  advanced: [],
212
276
  additionalProperties: false,
213
277
  };
@@ -434,23 +498,33 @@ describe('DynamicSchema', () => {
434
498
  });
435
499
  });
436
500
  describe('validate', () => {
437
- it('should return a Right undefined when the value matches the schema', () => {
501
+ it('should return a Right with the value when the value matches the schema', () => {
438
502
  const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
439
503
  if (E.isRight(schema)) {
440
504
  const result = DynamicSchema_1.DynamicSchema.validate(schema.right, value, O.none);
441
505
  expect(E.isRight(result)).toBe(true);
442
506
  if (E.isRight(result)) {
443
- expect(result.right).toBeUndefined();
507
+ expect(result.right).toEqual(value);
444
508
  }
445
509
  }
446
510
  });
447
- it('should return a Right undefined when strict is false and value matches the schema', () => {
511
+ it('should return a Right with the value when strict is false and value matches the schema', () => {
448
512
  const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
449
513
  if (E.isRight(schema)) {
450
514
  const result = DynamicSchema_1.DynamicSchema.validate(schema.right, value, O.some(false));
451
515
  expect(E.isRight(result)).toBe(true);
452
516
  if (E.isRight(result)) {
453
- expect(result.right).toBeUndefined();
517
+ expect(result.right).toEqual(value);
518
+ }
519
+ }
520
+ });
521
+ it('should return a Right with non extra values when value matches the schema but has extra fields', () => {
522
+ const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
523
+ if (E.isRight(schema)) {
524
+ const result = DynamicSchema_1.DynamicSchema.validate(schema.right, valueWithExtraFields, O.some(false));
525
+ expect(E.isRight(result)).toBe(true);
526
+ if (E.isRight(result)) {
527
+ expect(result.right).toEqual(value);
454
528
  }
455
529
  }
456
530
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/commons",
3
- "version": "4.6.0",
3
+ "version": "4.7.0",
4
4
  "description": "Extensions to the standard/core libraries and basic features",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"