Functions

@system-inc/base-foundation · 515c140 · 4 symbols

Find a registered class constructor by its name.

Used by the CLI when emitting GraphQL schema metadata: we have a type name string (the GraphQL input type) and need to resolve it to the corresponding TypeScript class so we can read its rules.

Returns undefined when no registered target matches.

findValidationTargetByName(name: string): Function | undefined

View source ↗

Return every rule registered against target and its ancestors, in the public shape that omits internal callbacks.

getValidationRules(target: Function): PublicRuleMetadata[]

View source ↗

registerRule

function

Creates a property decorator for a validation rule.

The returned factory is callable (VerifyIsEmail() / VerifyMaxLength(255)) and also exposes a .check() predicate for ad-hoc use:

VerifyIsEmail.check('foo@bar.com');       // true
VerifyMaxLength.check('abc', 255);         // true

Usage:

// No options:
export const VerifyIsString = registerRule<void>({
    name: 'IsString',
    check: (value) => typeof value === 'string',
    defaultMessage: ({ property }) => `${property} must be a string`,
});

// With options:
export const VerifyMaxLength = registerRule<number>({
    name: 'MaxLength',
    check: (value, max) =>
        typeof value === 'string' && value.length <= max,
    defaultMessage: ({ property, options }) =>
        `${property} must be at most ${options} characters`,
});

// Array-level:
export const VerifyArrayMinSize = registerRule<number>({
    name: 'ArrayMinSize',
    operatesOn: 'array',
    check: (value, min) => Array.isArray(value) && value.length >= min,
    defaultMessage: ({ property, options }) =>
        `${property} must contain at least ${options} elements`,
});
registerRule(definition: RuleDefinition<TOptions>): RuleDecorator<TOptions>

View source ↗

validate

function

Validates a value against the rules attached to its class and any nested validatable classes it contains.

Behavior:

  • Deep by default. Any property whose value is a class instance with registered rules is validated recursively. Same for arrays of such instances. Callers never need to annotate nested types.
  • Arrays are declared, then value rules iterate. An array-typed property must carry an array-level rule (operatesOn: 'array' — e.g. IsArray, ArrayMinSize), which receives the whole array. Only then do value-level rules apply per-element (error paths include the index, items[2].email). Without an array-level rule a value rule evaluates the value itself, so an array on a scalar property is rejected rather than silently iterated.
  • IsOptional short-circuits. When a property carries @VerifyIsOptional() and its value is null / undefined, all other rules on the property are skipped.
  • Cycles are safe. A visited set prevents infinite recursion through self-referential object graphs.

Returns a flat array of ValidationError. An empty array means the value passed validation.

validate(value: unknown): Promise<ValidationError[]>

View source ↗