@trayio/commons 4.37.0 → 4.39.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.
@@ -10,6 +10,8 @@ export interface DynamicSchemaBrand {
10
10
  export interface DynamicSchemaInterface {
11
11
  fromDynamicObject: (schema: DynamicObject, strict?: O.Option<boolean>) => E.Either<Error, DynamicSchema>;
12
12
  validate: (schema: DynamicSchema | DynamicObject, value: DynamicObject, strict?: O.Option<boolean>, coerce?: boolean) => E.Either<Error, DynamicObject>;
13
+ fromValue: (value: DynamicObject, schemaUrl: O.Option<string>) => DynamicObject;
14
+ isValidJsonSchema: (schema: DynamicObject) => boolean;
13
15
  }
14
16
  export declare const DynamicSchema: DynamicSchemaInterface;
15
17
  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;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,aA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AE,CAAC,MAAM,EAAE,aAAa,KAAK,OAAO,CAAC;CACtD;AAED,eAAO,MAAM,aAAa,EAAE,sBAgD3B,CAAC"}
@@ -32,11 +32,13 @@ const ajv_formats_1 = __importDefault(require("ajv-formats"));
32
32
  const E = __importStar(require("fp-ts/Either"));
33
33
  const O = __importStar(require("fp-ts/Option"));
34
34
  const t = __importStar(require("io-ts"));
35
- const function_1 = require("fp-ts/function");
35
+ const function_1 = require("fp-ts/lib/function");
36
36
  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 draft04SchemaUrl = 'http://json-schema.org/draft-04/schema';
41
+ const schemaProperty = '$schema';
40
42
  const ajv = new ajv_draft_04_1.default({ strict: false, removeAdditional: 'all' });
41
43
  /*
42
44
  We define additional formats for the JSON schema
@@ -92,6 +94,44 @@ const preprocessSchema = (schema, strict) => {
92
94
  preprocessSchema(schema.items, strict);
93
95
  }
94
96
  };
97
+ const createSchema = (value) => {
98
+ if (Array.isArray(value) && value.length > 0) {
99
+ return {
100
+ type: 'array',
101
+ items: createSchema(value[0]),
102
+ };
103
+ }
104
+ if (typeof value === 'object' && value !== null) {
105
+ const properties = {};
106
+ for (const [key, val] of Object.entries(value)) {
107
+ properties[key] = createSchema(val);
108
+ }
109
+ return {
110
+ type: 'object',
111
+ properties,
112
+ required: Object.keys(properties),
113
+ };
114
+ }
115
+ if (value === null) {
116
+ return { type: 'null' };
117
+ }
118
+ if (typeof value === 'boolean') {
119
+ return { type: 'boolean' };
120
+ }
121
+ if (typeof value === 'number') {
122
+ return { type: 'number' };
123
+ }
124
+ if (typeof value === 'string') {
125
+ return { type: 'string' };
126
+ }
127
+ return {};
128
+ };
129
+ const createSchemaWithSchemaUrl = (schema, schemaUrl) => {
130
+ const hasSchemaUrl = O.isSome(schemaUrl);
131
+ const schemaObj = createSchema(schema);
132
+ schemaObj[schemaProperty] = hasSchemaUrl ? schemaUrl.value : draft04SchemaUrl;
133
+ return schemaObj;
134
+ };
95
135
  exports.DynamicSchema = {
96
136
  fromDynamicObject: (schema, strict = O.none) => {
97
137
  const copiedSchema = (0, deep_copy_ts_1.deepCopy)(schema);
@@ -118,4 +158,6 @@ exports.DynamicSchema = {
118
158
  return E.left(new Error(errorText));
119
159
  }));
120
160
  },
161
+ fromValue: (value, schemaUrl = O.none) => createSchemaWithSchemaUrl(value, schemaUrl),
162
+ isValidJsonSchema: (schema) => isDynamicSchema(schema),
121
163
  };
@@ -30,6 +30,7 @@ describe('DynamicSchema', () => {
30
30
  let validSchema;
31
31
  let invalidSchema;
32
32
  let value;
33
+ let validSchemaWithoutExtraFields;
33
34
  let valueWithExtraFields;
34
35
  let valueWithCoerceableTypes;
35
36
  let invalidValue;
@@ -67,6 +68,40 @@ describe('DynamicSchema', () => {
67
68
  required: ['name', 'age', 'races'],
68
69
  advanced: [], // not part of json schema but the validator should not be strict with extra elements
69
70
  };
71
+ validSchemaWithoutExtraFields = {
72
+ type: 'object',
73
+ properties: {
74
+ name: {
75
+ type: 'string',
76
+ },
77
+ age: {
78
+ type: 'number',
79
+ },
80
+ races: {
81
+ type: 'array',
82
+ items: {
83
+ type: 'object',
84
+ properties: {
85
+ name: {
86
+ type: 'string',
87
+ },
88
+ car: {
89
+ type: 'object',
90
+ properties: {
91
+ model: {
92
+ type: 'string',
93
+ },
94
+ },
95
+ required: ['model'],
96
+ },
97
+ },
98
+ required: ['name', 'car'],
99
+ },
100
+ },
101
+ },
102
+ required: ['name', 'age', 'races'],
103
+ $schema: 'http://json-schema.org/draft-04/schema',
104
+ };
70
105
  invalidSchema = {
71
106
  type: 'invalidType',
72
107
  properties: {
@@ -574,4 +609,15 @@ describe('DynamicSchema', () => {
574
609
  }
575
610
  });
576
611
  });
612
+ describe('fromValue', () => {
613
+ it('should return a Right with the schema when given a valid value object', () => {
614
+ const result = DynamicSchema_1.DynamicSchema.fromValue(value, O.none);
615
+ expect(result).toEqual(validSchemaWithoutExtraFields);
616
+ });
617
+ it('should return a Right with the schema when given a valid value object and a schema url', () => {
618
+ const schemaUrl = 'http://json-schema.org/draft-04/schema#';
619
+ const result = DynamicSchema_1.DynamicSchema.fromValue(value, O.some(schemaUrl));
620
+ expect(result.$schema).toEqual(schemaUrl);
621
+ });
622
+ });
577
623
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/commons",
3
- "version": "4.37.0",
3
+ "version": "4.39.0",
4
4
  "description": "Extensions to the standard/core libraries and basic features",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"