json-schema-to-joi
Version:
Converts JSON schema to Joi typescript code
45 lines • 1.64 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateAnyJoi = exports.resolveJoiAnyMeta = void 0;
const _ = require("lodash");
const generate_1 = require("./generate");
function resolveJoiAnyMeta(joiAny, jsonSchema) {
(!!jsonSchema.description) && (joiAny.description = jsonSchema.description);
(!!jsonSchema.title) && (joiAny.label = _.camelCase(jsonSchema.title));
}
exports.resolveJoiAnyMeta = resolveJoiAnyMeta;
function generateAnyJoi(schema) {
let content = (schema.type === 'any') ? generate_1.openJoi([200, 'any()']) : [];
if (schema.allow) {
content.push(`.allow(...${JSON.stringify(schema.allow)})`);
}
if (schema.valid) {
content.push(`.valid(...${JSON.stringify(schema.valid)})`);
}
if (schema.invalid) {
content.push(`.invalid(...${JSON.stringify(schema.invalid)})`);
}
if (schema.default) {
content.push(`.default(${JSON.stringify(schema.default)})`);
}
content = generateBooleanKeys(schema, content);
if (schema.id) {
content.push(`.id(${JSON.stringify(schema.id)})`);
}
return (schema.type === 'any') ? generate_1.closeJoi(content) : content;
}
exports.generateAnyJoi = generateAnyJoi;
function generateBooleanKeys(schema, content) {
_.keys(schema).forEach((key) => {
if (key !== 'default') {
if (schema[key] === true) {
content.push(`.${key}()`);
}
else if (schema[key] === false) {
content.push(`.${key}(false)`);
}
}
});
return content;
}
//# sourceMappingURL=any.js.map
;