@trayio/commons
Advanced tools
Comparing version 4.6.0 to 4.7.0
@@ -12,3 +12,3 @@ import * as E from 'fp-ts/Either'; | ||
fromDynamicObject: (schema: DynamicObject, strict?: O.Option<boolean>) => E.Either<Error, DynamicSchema>; | ||
validate: (schema: DynamicSchema | DynamicObject, value: DynamicObject, strict?: O.Option<boolean>) => E.Either<Error, undefined>; | ||
validate: (schema: DynamicSchema | DynamicObject, value: DynamicObject, strict?: O.Option<boolean>) => E.Either<Error, DynamicObject>; | ||
} | ||
@@ -15,0 +15,0 @@ export declare const DynamicSchema: DynamicSchemaInterface; |
@@ -40,3 +40,3 @@ "use strict"; | ||
const Try_1 = require("../try/Try"); | ||
const ajv = new ajv_draft_04_1.default({ strict: false }); | ||
const ajv = new ajv_draft_04_1.default({ strict: false, removeAdditional: 'all' }); | ||
/* | ||
@@ -99,6 +99,7 @@ We define additional formats for the JSON schema | ||
preprocessSchema(copiedSchema, strict); | ||
const copiedValue = (0, deep_copy_ts_1.deepCopy)(value); | ||
const validateE = (0, Try_1.tryToEither)(() => ajv.compile(copiedSchema)); | ||
return (0, function_1.pipe)(validateE, E.chain((validate) => { | ||
if (validate(value)) { | ||
return E.right(undefined); | ||
if (validate(copiedValue)) { | ||
return E.right(copiedValue); | ||
} | ||
@@ -105,0 +106,0 @@ return E.left(new Error(ajv.errorsText(validate.errors))); |
@@ -33,2 +33,3 @@ "use strict"; | ||
let value; | ||
let valueWithExtraFields; | ||
beforeEach(() => { | ||
@@ -44,4 +45,23 @@ validSchema = { | ||
}, | ||
races: { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string', | ||
}, | ||
car: { | ||
type: 'object', | ||
properties: { | ||
model: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
required: ['name', 'age'], | ||
required: ['name', 'age', 'races'], | ||
advanced: [], // not part of json schema but the validator should not be strict with extra elements | ||
@@ -64,3 +84,26 @@ }; | ||
age: 30, | ||
races: [ | ||
{ | ||
name: 'monza', | ||
car: { | ||
model: 'ferrari', | ||
}, | ||
}, | ||
], | ||
}; | ||
valueWithExtraFields = { | ||
name: 'John Doe', | ||
age: 30, | ||
extra: 'field', | ||
races: [ | ||
{ | ||
name: 'monza', | ||
country: 'italy', | ||
car: { | ||
model: 'ferrari', | ||
year: 2017, | ||
}, | ||
}, | ||
], | ||
}; | ||
}); | ||
@@ -212,4 +255,25 @@ describe('fromDynamicObject', () => { | ||
}, | ||
races: { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
name: { | ||
type: 'string', | ||
}, | ||
car: { | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
model: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
required: ['name', 'age'], | ||
required: ['name', 'age', 'races'], | ||
advanced: [], | ||
@@ -441,3 +505,3 @@ additionalProperties: false, | ||
describe('validate', () => { | ||
it('should return a Right undefined when the value matches the schema', () => { | ||
it('should return a Right with the value when the value matches the schema', () => { | ||
const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema); | ||
@@ -448,7 +512,7 @@ if (E.isRight(schema)) { | ||
if (E.isRight(result)) { | ||
expect(result.right).toBeUndefined(); | ||
expect(result.right).toEqual(value); | ||
} | ||
} | ||
}); | ||
it('should return a Right undefined when strict is false and value matches the schema', () => { | ||
it('should return a Right with the value when strict is false and value matches the schema', () => { | ||
const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema); | ||
@@ -459,6 +523,16 @@ if (E.isRight(schema)) { | ||
if (E.isRight(result)) { | ||
expect(result.right).toBeUndefined(); | ||
expect(result.right).toEqual(value); | ||
} | ||
} | ||
}); | ||
it('should return a Right with non extra values when value matches the schema but has extra fields', () => { | ||
const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema); | ||
if (E.isRight(schema)) { | ||
const result = DynamicSchema_1.DynamicSchema.validate(schema.right, valueWithExtraFields, O.some(false)); | ||
expect(E.isRight(result)).toBe(true); | ||
if (E.isRight(result)) { | ||
expect(result.right).toEqual(value); | ||
} | ||
} | ||
}); | ||
it('should return a Left Error when the value does not match the schema', () => { | ||
@@ -465,0 +539,0 @@ const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema); |
{ | ||
"name": "@trayio/commons", | ||
"version": "4.6.0", | ||
"version": "4.7.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
146126
2928