UNPKG

vulcain-corejs

Version:
135 lines (134 loc) 3.92 kB
import { IContainer } from '../di/resolvers'; import { PropertyOptions } from './annotations'; import { ReferenceOptions } from './annotations'; import { RequestContext } from '../servers/requestContext'; export interface ErrorMessage { message: string; id: string; property?: string; } export interface SchemaDescription { name: string; properties: { [index: string]: PropertyOptions; }; references: { [index: string]: ReferenceOptions; }; extends?: SchemaDescription | string; hasSensibleData?: boolean; bind?: (obj) => any; validate?: (val, ctx: RequestContext) => string; storageName?: string; idProperty?: string; } /** * Schema definition */ export declare class Schema { name: string; description: SchemaDescription; private _domain; /** * Current domain model * @returns {Domain} */ readonly domain: Domain; readonly extends: SchemaDescription; /** * Create a new schema * @param domain : current domain model * @param name : schema name or schema */ constructor(domain: Domain, name: string); /** * * @param origin * @returns {null|any|{}} */ bind(origin: any, old?: any): any; validateAsync(ctx: RequestContext, obj: any): Promise<ErrorMessage[]>; getIdProperty(): string; getId(obj: any): any; encrypt(entity: any): any; decrypt(entity: any): any; /** * Copy an entity to another taking into account references defined in schema * * @param {any} target * @param {any} source * @param {SchemaDescription} [schema] * @returns * * @memberOf Schema */ deepAssign(target: any, source: any, schema?: SchemaDescription): any; } /** * Domain model */ export declare class Domain { name: string; private container; private _schemaDescriptions; private types; private builder; constructor(name: string, container: IContainer, defaultTypes?: any); addSchemaDescription(schema: any, name?: string): string; /** * Get a registered schema by name * Throws an exception if not exists * @param {string} schema name * @returns a schema */ getSchema(name: string | Function, optional?: boolean): Schema; /** * Get all schemas * * @readonly */ readonly schemas: SchemaDescription[]; /** * Do not use directly * * @param {string} name * @returns */ findSchemaDescription(name: string): SchemaDescription; /** * Do not use directly * * @readonly */ readonly schemaDescriptions: SchemaDescription[]; addTypes(types: any, ns?: string): void; addType(name: string, type: string, info: any, ns?: string): void; _findType(name: string): any; /** * Remove all sensible data * * @param {any} entity * @param {any} schemaName * @returns */ obfuscate(entity: any, schema: Schema): void; /** * Convert a new object from an other based on a specific schema * @param origin : initial object to convert * @param schemaName : schema to used (default=current schema) * @param obj : existing object to use * @returns {any} */ bind(origin: any, schemaName?: string | SchemaDescription, obj?: any): any; private applyBinding(prop, origin, val); private isMany(relSchema); /** * Validate an object * @param val : Object to validate * @param schemaName : schema to use (default=current schema) * @returns Array<string> : A list of errors */ validateAsync(ctx: RequestContext, val: any, schemaName?: string | SchemaDescription): Promise<ErrorMessage[]>; resolveSchemaDescription(schemaName: string | SchemaDescription, val?: any): SchemaDescription; getIdProperty(schemaName?: string | SchemaDescription): string; }