ottoman
Version:
Ottoman Couchbase ODM
101 lines (100 loc) • 3.32 kB
TypeScript
import { Schema } from '../schema';
import { CustomValidations, FactoryFunction, FieldMap, SchemaDef } from '../interfaces/schema.types';
import { CastOptions } from '../../utils/cast-strategy';
/**
* Build the fields using the given definition.
* If the [[obj]] is a schema instance, the data will be taken from the fields provided.
* @function
* @public
*
* @param {Schema|Object} obj the definition or schema instance
* @param strict will drop all properties not defined in the schema
* @returns {FieldMap}
* @throws {Error}
*
* @example
* ```ts
* const fields = buildFields({ name: String, hasChild: { type: Boolean, default: true } });
* ```
*/
export declare const buildFields: (obj: Schema | SchemaDef, strict?: boolean) => FieldMap;
/**
* Validate data using the schema definition.
* @param data that is going to be validated
* @param schema that will be used to validate
* @param options
* @throws BuildSchemaError, ValidationError
* @example
* ```ts
* const data = {
* name: "John",
* age: "50"
* };
* const schema = new Schema({
* name: String,
* age: {type: Number, intVal: true}
* });
* const strictSchema = new Schema({
* name: String,
* age: {type: Number, intVal: true}
* },
* {
* validationStrategy: VALIDATION_STRATEGY.STRICT
* });
* console.log(castSchema(data, schema)); // Print { name: "John", age: 50 }
* console.log(castSchema(data, strictSchema)); // Throw "Property age must be of type Number"
* ```
*/
export declare const validate: (data: any, schema: Schema | SchemaDef, options?: CastOptions) => any;
/**
* Apply default values defined on schema to an object instance.
* @param obj reference to object instance
* @param schema definition will be used to determine default definitions
*
* @example
* ```ts
* const schema = { name: { type: String, default: 'John' }, hasChild: { type: Boolean, default: true } };
* const obj: any = applyDefaultValue(obj, schema)
*
* console.log(obj);
* ```
*/
export declare const applyDefaultValue: (obj: any, schema: Schema | SchemaDef) => any;
/**
* Register a custom type to Schema supported types.
* @function
* @param name
* @param factory
* @throws Error
* @example
* ```ts
* registerType(Int8.name, (fieldName, opts) => new Int8(fieldName, opts.required));
* ```
*/
export declare const registerType: (name: string, factory: FactoryFunction) => void;
/**
* Register custom validators to Schema validators register.
* @function
* @param validators
* @throws Error
* @example
* ```ts
* addValidators({
* email: (value) => {
* regexp = new RegExp(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);
* if (!regexp.test(value)) {
* throw new Error('Email address is invalid.')
* }
* }
* });
*
* const ContactSchema = new Schema({
* name: String,
* contact: { type: String, validator: 'email' }
* });
* ```
*/
export declare const addValidators: (validators: CustomValidations) => void;
export declare const mergeHooks: (hook: Record<string, unknown[]>, other: Record<string, unknown[]>) => {
[x: string]: unknown[];
};