mikro-orm/mikro-orm

View on GitHub
packages/core/src/decorators/Check.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { MetadataStorage } from '../metadata';
import { Utils } from '../utils/Utils';
import type { CheckConstraint, Dictionary } from '../typings';

export function Check<T>(options: CheckOptions<T>) {
  return function (target: any, propertyName?: string) {
    const meta = MetadataStorage.getMetadataFromDecorator<T>((propertyName ? target.constructor : target) as T & Dictionary);
    options.property ??= propertyName;
    meta.checks.push(options);

    if (!propertyName) {
      return target;
    }

    return Utils.propertyDecoratorReturnValue();
  };
}

export type CheckOptions<T = any> = CheckConstraint<T>;