@trayio/commons
Advanced tools
Comparing version 4.3.0 to 4.4.0
@@ -7,3 +7,3 @@ import * as E from 'fp-ts/Either'; | ||
export type DynamicSchema = t.TypeOf<typeof dynamicSchemaTypeDescriptor>; | ||
export declare const recursivelyAddAdditionalProperties: (schema: DynamicObject, additionalProperties: boolean) => void; | ||
export declare const recursivelyRemoveEmptyRequiredAndAddAdditionalProperties: (schema: DynamicObject, strict: O.Option<boolean>) => void; | ||
export interface DynamicSchemaBrand { | ||
@@ -10,0 +10,0 @@ readonly DynamicSchema: unique symbol; |
@@ -29,3 +29,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DynamicSchema = exports.recursivelyAddAdditionalProperties = void 0; | ||
exports.DynamicSchema = exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties = void 0; | ||
const ajv_draft_04_1 = __importDefault(require("ajv-draft-04")); | ||
@@ -58,3 +58,4 @@ const ajv_formats_1 = __importDefault(require("ajv-formats")); | ||
const dynamicSchemaTypeCodec = TypeCodec_1.TypeCodec.fromDescriptor(dynamicSchemaTypeDescriptor); | ||
const recursivelyAddAdditionalProperties = (schema, additionalProperties) => { | ||
const recursivelyRemoveEmptyRequiredAndAddAdditionalProperties = (schema, strict) => { | ||
const hasStrict = O.isSome(strict); | ||
if (schema.oneOf || schema.anyOf || schema.allOf) { | ||
@@ -65,7 +66,14 @@ const schemas = (schema.oneOf || | ||
schemas.forEach((subSchema) => { | ||
(0, exports.recursivelyAddAdditionalProperties)(subSchema, additionalProperties); | ||
(0, exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(subSchema, strict); | ||
}); | ||
} | ||
if (schema.properties) { | ||
schema.additionalProperties = additionalProperties; | ||
if (hasStrict) { | ||
schema.additionalProperties = !strict.value; | ||
} | ||
if (schema.required && | ||
Array.isArray(schema.required) && | ||
schema.required.length === 0) { | ||
delete schema.required; | ||
} | ||
const properties = schema.properties; | ||
@@ -75,3 +83,3 @@ Object.keys(properties).forEach((key) => { | ||
if (property.type === 'object' || property.type === 'array') { | ||
(0, exports.recursivelyAddAdditionalProperties)(property, additionalProperties); | ||
(0, exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(property, strict); | ||
} | ||
@@ -81,15 +89,11 @@ }); | ||
if (schema.type === 'array') { | ||
(0, exports.recursivelyAddAdditionalProperties)(schema.items, additionalProperties); | ||
(0, exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema.items, strict); | ||
} | ||
}; | ||
exports.recursivelyAddAdditionalProperties = recursivelyAddAdditionalProperties; | ||
exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties = recursivelyRemoveEmptyRequiredAndAddAdditionalProperties; | ||
exports.DynamicSchema = { | ||
fromDynamicObject: (schemaObj) => (0, function_1.pipe)(dynamicSchemaTypeCodec.decode(schemaObj), E.mapLeft((e) => new Error(`Failed to create DynamicSchema: ${e}`))), | ||
validate: (schema, value, strict) => { | ||
let copiedSchema = schema; | ||
if (O.isSome(strict)) { | ||
copiedSchema = (0, deep_copy_ts_1.deepCopy)(schema); | ||
const hasAdditionalProperties = !strict.value; | ||
(0, exports.recursivelyAddAdditionalProperties)(copiedSchema, hasAdditionalProperties); | ||
} | ||
const copiedSchema = (0, deep_copy_ts_1.deepCopy)(schema); | ||
(0, exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(copiedSchema, strict); | ||
const validateE = (0, Try_1.tryToEither)(() => ajv.compile(copiedSchema)); | ||
@@ -96,0 +100,0 @@ return (0, function_1.pipe)(validateE, E.chain((validate) => { |
@@ -114,5 +114,17 @@ "use strict"; | ||
}); | ||
describe('recursivelyAddAdditionalProperties', () => { | ||
it('should correctly add additionalProperties=true to object schema', () => { | ||
const schema = validSchema; | ||
describe('recursivelyRemoveEmptyRequiredAndAddAdditionalProperties', () => { | ||
it('should correctly remove empty required arrays and not add additionalProperties when strict is not set', () => { | ||
const schema = { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string', | ||
}, | ||
age: { | ||
type: 'number', | ||
}, | ||
}, | ||
required: [], | ||
advanced: [], | ||
}; | ||
const expectedSchema = { | ||
@@ -128,7 +140,65 @@ type: 'object', | ||
}, | ||
required: ['name', 'age'], | ||
advanced: [], | ||
}; | ||
(0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.none); | ||
expect(schema).toEqual(expectedSchema); | ||
}); | ||
it('should correctly keep populated required arrays and not add additionalProperties when strict is not set', () => { | ||
const schema = { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string', | ||
}, | ||
age: { | ||
type: 'number', | ||
}, | ||
}, | ||
required: ['age', 'name'], | ||
advanced: [], | ||
}; | ||
const expectedSchema = { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string', | ||
}, | ||
age: { | ||
type: 'number', | ||
}, | ||
}, | ||
required: ['age', 'name'], | ||
advanced: [], | ||
}; | ||
(0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.none); | ||
expect(schema).toEqual(expectedSchema); | ||
}); | ||
it('should correctly remove empty required and add additionalProperties=true to object schema when strict is set', () => { | ||
const schema = { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string', | ||
}, | ||
age: { | ||
type: 'number', | ||
}, | ||
}, | ||
required: [], | ||
advanced: [], | ||
}; | ||
const expectedSchema = { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string', | ||
}, | ||
age: { | ||
type: 'number', | ||
}, | ||
}, | ||
advanced: [], | ||
additionalProperties: true, | ||
}; | ||
(0, DynamicSchema_1.recursivelyAddAdditionalProperties)(schema, true); | ||
(0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(false)); | ||
expect(schema).toEqual(expectedSchema); | ||
@@ -152,3 +222,3 @@ }); | ||
}; | ||
(0, DynamicSchema_1.recursivelyAddAdditionalProperties)(schema, false); | ||
(0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(true)); | ||
expect(schema).toEqual(expectedSchema); | ||
@@ -220,3 +290,3 @@ }); | ||
}; | ||
(0, DynamicSchema_1.recursivelyAddAdditionalProperties)(schema, true); | ||
(0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(false)); | ||
expect(schema).toEqual(expectedSchema); | ||
@@ -228,3 +298,3 @@ }); | ||
}; | ||
(0, DynamicSchema_1.recursivelyAddAdditionalProperties)(schema, false); | ||
(0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(true)); | ||
expect(schema).toEqual(schema); | ||
@@ -277,3 +347,3 @@ }); | ||
}; | ||
(0, DynamicSchema_1.recursivelyAddAdditionalProperties)(schema, true); | ||
(0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(false)); | ||
expect(schema).toEqual(expectedSchema); | ||
@@ -326,3 +396,3 @@ }); | ||
}; | ||
(0, DynamicSchema_1.recursivelyAddAdditionalProperties)(schema, true); | ||
(0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(false)); | ||
expect(schema).toEqual(expectedSchema); | ||
@@ -375,3 +445,3 @@ }); | ||
}; | ||
(0, DynamicSchema_1.recursivelyAddAdditionalProperties)(schema, true); | ||
(0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(false)); | ||
expect(schema).toEqual(expectedSchema); | ||
@@ -378,0 +448,0 @@ }); |
{ | ||
"name": "@trayio/commons", | ||
"version": "4.3.0", | ||
"version": "4.4.0", | ||
"description": "Extensions to the standard/core libraries and basic features", | ||
@@ -5,0 +5,0 @@ "exports": { |
Sorry, the diff of this file is not supported yet
142703
2824