@trayio/commons
Advanced tools
Comparing version 4.37.0 to 4.38.0
@@ -13,2 +13,4 @@ import * as E from 'fp-ts/Either'; | ||
validate: (schema: DynamicSchema | DynamicObject, value: DynamicObject, strict?: O.Option<boolean>, coerce?: boolean) => E.Either<Error, DynamicObject>; | ||
fromValue: (value: DynamicObject, schemaUrl: O.Option<string>) => DynamicObject; | ||
isValidJsonSchema: (schema: DynamicObject) => boolean; | ||
} | ||
@@ -15,0 +17,0 @@ export declare const DynamicSchema: DynamicSchemaInterface; |
@@ -35,3 +35,3 @@ "use strict"; | ||
const t = __importStar(require("io-ts")); | ||
const function_1 = require("fp-ts/function"); | ||
const function_1 = require("fp-ts/lib/function"); | ||
const deep_copy_ts_1 = require("deep-copy-ts"); | ||
@@ -41,2 +41,4 @@ const DynamicType_1 = require("../dynamictype/DynamicType"); | ||
const Try_1 = require("../try/Try"); | ||
const draft04SchemaUrl = 'http://json-schema.org/draft-04/schema'; | ||
const schemaProperty = '$schema'; | ||
const ajv = new ajv_draft_04_1.default({ strict: false, removeAdditional: 'all' }); | ||
@@ -97,2 +99,40 @@ /* | ||
}; | ||
const createSchema = (value) => { | ||
if (Array.isArray(value) && value.length > 0) { | ||
return { | ||
type: 'array', | ||
items: createSchema(value[0]), | ||
}; | ||
} | ||
if (typeof value === 'object' && value !== null) { | ||
const properties = {}; | ||
for (const [key, val] of Object.entries(value)) { | ||
properties[key] = createSchema(val); | ||
} | ||
return { | ||
type: 'object', | ||
properties, | ||
required: Object.keys(properties), | ||
}; | ||
} | ||
if (value === null) { | ||
return { type: 'null' }; | ||
} | ||
if (typeof value === 'boolean') { | ||
return { type: 'boolean' }; | ||
} | ||
if (typeof value === 'number') { | ||
return { type: 'number' }; | ||
} | ||
if (typeof value === 'string') { | ||
return { type: 'string' }; | ||
} | ||
return {}; | ||
}; | ||
const createSchemaWithSchemaUrl = (schema, schemaUrl) => { | ||
const hasSchemaUrl = O.isSome(schemaUrl); | ||
const schemaObj = createSchema(schema); | ||
schemaObj[schemaProperty] = hasSchemaUrl ? schemaUrl.value : draft04SchemaUrl; | ||
return schemaObj; | ||
}; | ||
exports.DynamicSchema = { | ||
@@ -124,2 +164,4 @@ fromDynamicObject: (schema, strict = O.none) => { | ||
}, | ||
fromValue: (value, schemaUrl = O.none) => createSchemaWithSchemaUrl(value, schemaUrl), | ||
isValidJsonSchema: (schema) => isDynamicSchema(schema), | ||
}; |
@@ -33,2 +33,3 @@ "use strict"; | ||
let value; | ||
let validSchemaWithoutExtraFields; | ||
let valueWithExtraFields; | ||
@@ -71,2 +72,36 @@ let valueWithCoerceableTypes; | ||
}; | ||
validSchemaWithoutExtraFields = { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string', | ||
}, | ||
age: { | ||
type: 'number', | ||
}, | ||
races: { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string', | ||
}, | ||
car: { | ||
type: 'object', | ||
properties: { | ||
model: { | ||
type: 'string', | ||
}, | ||
}, | ||
required: ['model'], | ||
}, | ||
}, | ||
required: ['name', 'car'], | ||
}, | ||
}, | ||
}, | ||
required: ['name', 'age', 'races'], | ||
$schema: 'http://json-schema.org/draft-04/schema', | ||
}; | ||
invalidSchema = { | ||
@@ -579,2 +614,13 @@ type: 'invalidType', | ||
}); | ||
describe('fromValue', () => { | ||
it('should return a Right with the schema when given a valid value object', () => { | ||
const result = DynamicSchema_1.DynamicSchema.fromValue(value, O.none); | ||
expect(result).toEqual(validSchemaWithoutExtraFields); | ||
}); | ||
it('should return a Right with the schema when given a valid value object and a schema url', () => { | ||
const schemaUrl = 'http://json-schema.org/draft-04/schema#'; | ||
const result = DynamicSchema_1.DynamicSchema.fromValue(value, O.some(schemaUrl)); | ||
expect(result.$schema).toEqual(schemaUrl); | ||
}); | ||
}); | ||
}); |
{ | ||
"name": "@trayio/commons", | ||
"version": "4.37.0", | ||
"version": "4.38.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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
194533
4004