joiful
Version:
TypeScript Declarative Validation. Decorate your class properties to validate them using Joi.
85 lines • 3.43 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Joiful = void 0;
const any_1 = require("./decorators/any");
const array_1 = require("./decorators/array");
const boolean_1 = require("./decorators/boolean");
const date_1 = require("./decorators/date");
const function_1 = require("./decorators/function");
const link_1 = require("./decorators/link");
const number_1 = require("./decorators/number");
const object_1 = require("./decorators/object");
const string_1 = require("./decorators/string");
const validation_1 = require("./validation");
const core_1 = require("./core");
class Joiful {
constructor(options = {}) {
this.options = options;
/**
* Property decorator that allows the property to be any type.
*/
this.any = () => any_1.createAnyPropertyDecorator(this.options);
/**
* Property decorator that constrains the property to be an array.
*/
this.array = (options) => array_1.createArrayPropertyDecorator(options, this.options);
/**
* Property decorator that constrains the property to be a boolean.
*/
this.boolean = () => boolean_1.createBooleanPropertyDecorator(this.options);
/**
* Property decorator that constrains the property to be a Date.
*/
this.date = () => date_1.createDatePropertyDecorator(this.options);
/**
* Property decorator that constrains the property to be a Function.
*/
this.func = () => function_1.createFunctionPropertyDecorator(this.options);
/**
* Property decorator that constrains the property to another schema.
* This allows defining classes that reference themself. e.g.
*
* @example
* class TreeNode {
* @jf.string().required()
* title: string;
*
* @jf.array().items((joi) => joi.link('...'))
* children: TreeNode[];
* }
*/
this.link = (ref) => link_1.createLinkPropertyDecorator(ref, this.options);
/**
* Property decorator that constrains the property to be a number.
*/
this.number = () => number_1.createNumberPropertyDecorator(this.options);
/**
* Property decorator that constrains the property to be an object.
*/
this.object = (options) => object_1.createObjectPropertyDecorator(options, this.options);
/**
* Property decorator that constrains the property to be a string.
*/
this.string = () => string_1.createStringPropertyDecorator(this.options);
/**
* Method decorator that validates the parameters passed into the method.
*/
this.validateParams = (options) => validation_1.createValidatePropertyDecorator(options);
/**
* Returns the Joi schema associated with a class or undefined if there isn't one.
*/
this.getSchema = (Class) => {
return core_1.getJoiSchema(Class, this.joi);
};
/**
* Returns whether the given class has a Joi schema associated with it
*/
this.hasSchema = (Class) => Boolean(this.getSchema(Class));
core_1.checkJoiIsCompatible(options.joi);
}
get joi() {
return core_1.getJoi(this.options);
}
}
exports.Joiful = Joiful;
//# sourceMappingURL=joiful.js.map