@creditkarma/dynamic-config
Version:
Dynamic Config for Node.js backed by Consul and Vault
51 lines • 1.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.objectAsSimpleSchema = exports.objectMatchesSchema = void 0;
const JsonValidator = require("ajv");
const JSON_VALIDATOR = new JsonValidator();
function objectMatchesSchema(schema, data) {
return JSON_VALIDATOR.validate(schema, data);
}
exports.objectMatchesSchema = objectMatchesSchema;
/**
* Creates a schema for the given object. The resulting schema is a simple JSON Schema.
*/
function objectAsSimpleSchema(obj) {
const objType = typeof obj;
if (Array.isArray(obj)) {
return {
type: 'array',
items: objectAsSimpleSchema(obj[0]),
};
}
else if (objType === 'object') {
const schema = {
type: 'object',
properties: {},
required: [],
};
if (obj !== null) {
for (const key of Object.keys(obj)) {
const propSchema = objectAsSimpleSchema(obj[key]);
schema.properties[key] = propSchema;
if (schema.required !== undefined &&
propSchema.type !== 'undefined') {
schema.required.push(key);
}
}
}
return schema;
}
else {
if (objType !== 'function' && objType !== 'symbol') {
return {
type: objType,
};
}
else {
throw new Error(`Type[${objType}] cannot be encoded to JSON`);
}
}
}
exports.objectAsSimpleSchema = objectAsSimpleSchema;
//# sourceMappingURL=json.js.map