UNPKG

@lucaspaganini/value-objects

Version:

TypeScript first validation and class creation library

103 lines (102 loc) 3.41 kB
import { ValueObjectContructor, ValueObjectWorkAround, VOCRaw, VOCRawInit } from './value-object'; export interface VOObjectOptions { /** * Maximum inclusive errors to acumulate before throwing. * Can't be less than zero. * @default 1 */ maxErrors?: number; } declare type VOObjectSchema<Schema> = { [P in keyof Schema]: ValueObjectContructor; }; declare type VOObjectRawInitSchema<Schema extends VOObjectSchema<Schema>> = { [P in keyof Schema]: Schema[P] extends ValueObjectContructor ? VOCRawInit<Schema[P]> : never; }; declare type VOObjectRawSchema<Schema extends VOObjectSchema<Schema>> = { [P in keyof Schema]: Schema[P] extends ValueObjectContructor ? VOCRaw<Schema[P]> : never; }; export declare type VOObjectInstance<Schema extends VOObjectSchema<Schema>> = ValueObjectWorkAround<VOObjectRawSchema<Schema>> & { [P in keyof Schema]: InstanceType<Schema[P]>; }; export interface VOObjectConstructor<Schema extends VOObjectSchema<Schema>> { new (rawInit: VOObjectRawInitSchema<Schema>): VOObjectInstance<Schema>; } /** * Function to create an object wrapper over a given map of value * object constructors. Useful if you have different classes and * want to aggregate them. * * @template Schema Object mapping to value object constructors. * @param schema Object mapping to value object constructors. * @param options Customizations for the returned class constructor. * @return Class constructor that accepts an object mapping it's keys * and values to what the inner value object constructors expect. * Calling {@link VOObjectInstance.valueOf} calls `valueOf()` for all it's inner instances * and returns them in an object. * * @example * ```typescript * class Email extends VOString({ ... }) { * getHost(): string { ... } * } * * class Name extends VOString({ ... }) {} * * class Password extends VOString({ ... }) {} * * class User extends VOObject({ * name: Name, * email: Email, * password: Password * }) {} * * new User({ * name: 'Lucas', * email: 'me@lucaspaganini.com', * password: 'Secret123' * }); // OK * * new User({ * name: 'Lucas', * email: 123, * password: 'Secret123' * }); // Compilation error: `.email` expects a string * * new User({ * name: 'Lucas', * email: 'lucaspaganini.com', * password: 'Secret123' * }); // Runtime error: `.email` Value doesn't match pattern * * const user = new User({ * name: 'Lucas', * email: 'me@lucaspaganini.com', * password: 'Secret123' * }); * * user.valueOf(); // { name: 'Lucas', email: 'me@lucaspaganini.com', password: 'Secret123' } * user.email.getHost(); // lucaspaganini.com * ``` * * @example * ```typescript * class Test { * constructor(shouldThrow: boolean) { * if (shouldThrow) throw Error('I was instructed to throw'); * } * } * new Test(false); // OK * new Test(true); // Runtime error: I was instructed to throw * * class TestsObject extends VoObject({ * aaa: Test, * bbb: Test, * ccc: Test * }, { maxErrors: 2 }) {} * new TestsArray({ aaa: false, bbb: false, ccc: false }); // OK * new TestsArray({ aaa: true, bbb: true, ccc: true }); // Runtime error: ["I was instructed to throw", "I was instructed to throw"] * ``` */ export declare const VOObject: <Schema extends VOObjectSchema<Schema>>(schema: Schema, options?: VOObjectOptions) => VOObjectConstructor<Schema>; export {};