@knorm/knorm
Version:
A JavaScript ORM written using ES6 classes
144 lines (143 loc) • 5.86 kB
TypeScript
/**
* Creates and holds configuration for fields, e.g. how to validate or cast
* fields.
*/
export class Field {
/**
* Creates a {@link Field} instance.
*
* @param {object} [config] The field's configuration.
*/
constructor(config?: object | undefined);
castors: any;
config: object;
name: any;
type: any;
path: any;
model: any;
updated: any;
column: any;
primary: boolean;
unique: boolean;
methods: boolean;
default: any;
validators: {
type: any;
};
_createValidators(config: any): {
type: any;
};
_createShapeValidators(shape: any): any;
getColumnName(fieldName: any): any;
throwValidationError(value: any, validator: any): void;
validateIsRequired(value: any): void;
_validateTypeWith(value: any, type: any, validate: any): true | undefined;
_validateIsAny(value: any, type: any): void;
validateIsAny(value: any, type: any): void;
validateIsNumber(value: any, type: any): void;
_validateIsString(value: any, type: any): void;
validateIsString(value: any, type: any): void;
validateIsText(value: any, type: any): void;
validateIsEmail(value: any, type: any): void;
validateIsBinary(value: any, type: any): void;
validateIsInteger(value: any, type: any): void;
validateIsBoolean(value: any, type: any): void;
_validateIsDate(value: any, type: any): void;
validateIsDate(value: any, type: any): void;
validateIsDateTime(value: any, type: any): void;
validateIsUuid(value: any, type: any): void;
validateIsUuidV4(value: any, type: any): void;
validateIsDecimal(value: any, type: any): void;
validateIsJson(value: any, type: any): void;
validateIsJsonB(value: any, type: any): void;
validateIsObject(value: any, type: any): void;
validateIsArray(value: any, type: any): void;
validateTypeIs(value: any, type: any): void;
validateMinLengthIs(value: any, minLength: any): true | undefined;
validateMaxLengthIs(value: any, maxLength: any): true | undefined;
validateIsOneOf(value: any, oneOf: any): true | undefined;
validateEquals(value: any, equals: any): true | undefined;
_validateRegexMatching(value: any, regex: any): void;
_validateRegexNotMatching(value: any, regex: any): void;
validateWithRegex(value: any, regex: any): true | void;
/**
* Custom validator function, note that `async` validator functions, or
* functions that return a {@link Promise}, are supported.
*
* Validation for the value will be failed if the function:
* - throws an error
* - returns `false`
* - returns a `Promise` that is rejected with an error
* - returns a `Promise` that is resolved with `false`
*
* This function may also return an object with the regular
* [validators](/guides/fields.md#field-config), or resolving the `Promise`
* with an object with validators, including another custom validator
* function!
*
* @callback Field~customValidator
* @param {any} value The value to validate.
* @param {Model} model The {@link Model} instance where the field value is
* set.
*
* @returns {Promise|boolean|object}
*/
/**
* Validates a value with a custom validator function.
*
* @param {any} value The value to validate
* @param {Field~customValidator} validate The validator function.
* @param {Model} model The {@link Model} instance where the field's value is
* set.
*
* @returns {Promise} A `Promise` that is resolved if the `value` param passes
* custom validation, or otherwise rejected.
*/
validateWithCustom(value: any, validate: any, model: Model): Promise<any>;
validateWithShape(value: any, shape: any, modelInstance: any): Promise<any>;
validateWithValidators(value: any, validators: any, modelInstance: any): Promise<void>;
validate(value: any, modelInstance: any): Promise<void>;
/**
* Casts a {@link Field}'s value with its configured cast functions.
*
* @param {any} value The value to be cast.
* @param {Model} model The {@link Model} instance where the field's
* value is set. The cast functions will be called with this instance as a
* parameter.
* @param {object} options Cast options.
* @param {boolean} options.forSave Whether or not to cast for save operations
* (i.e before insert or update). This function is called with the `value` as
* the first paramter and `model` as the second parameter.
* @param {boolean} options.forFetch Whether or not to cast for fetch
* operations (i.e after fetch or any other operations that return data from
* the database). This function is called with the `value` as the first
* paramter and `model` as the second parameter.
*
* @returns {any} The cast value, or `undefined` if no cast functions are
* configured for the {@link Field} instance.
*/
cast(value: any, model: Model, { forSave, forFetch }: {
forSave: boolean;
forFetch: boolean;
}): any;
/**
* Returns the default value for a field from the {@link Field}'s
* configuration.
*
* @param {Model} model The {@link Model} instance where the field's default
* value will be set. If the default is configured as a function, the function
* is called with `model` as the first parameter.
*
* @returns {any|undefined} The default value or `undefined` if there's no
* default value set for the field.
*/
getDefault(model: Model): any;
knorm: any;
}
export namespace Field {
export const knorm: any;
export const types: string[];
export { ValidationError };
}
import { Model } from "./Model";
import { ValidationError } from "./ValidationError";