UNPKG

@nestia/core

Version:

Super-fast validation decorators of NestJS

73 lines (67 loc) 1.96 kB
import { AssertProgrammer, MetadataCollection, MetadataFactory, MetadataSchema, TransformerError, } from "@typia/core"; import ts from "typescript"; import { INestiaTransformContext } from "../options/INestiaTransformProject"; export namespace PlainBodyProgrammer { export const generate = (props: { context: INestiaTransformContext; modulo: ts.LeftHandSideExpression; type: ts.Type; }): ts.Expression => { const result = MetadataFactory.analyze({ checker: props.context.checker, transformer: props.context.transformer, options: { escape: false, constant: true, absorb: true, validate: ({ metadata }) => validate(metadata), }, components: new MetadataCollection(), type: props.type, }); if (result.success === false) throw TransformerError.from({ code: "nestia.core.TypedParam", errors: result.errors, }); return AssertProgrammer.write({ context: { ...props.context, options: { numeric: false, finite: false, functional: false, }, }, modulo: props.modulo, config: { equals: false, guard: false, }, type: props.type, name: undefined, }); }; } const validate = (metadata: MetadataSchema): string[] => { const errors: string[] = []; const insert = (msg: string) => errors.push(msg); const expected: number = (metadata.atomics.some((a) => a.type === "string") ? 1 : 0) + metadata.templates.length + metadata.constants .filter((c) => c.type === "string") .map((c) => c.values.length) .reduce((a, b) => a + b, 0); if (expected === 0 || expected !== metadata.size()) insert(`only string type is allowed`); if (metadata.nullable === true) insert(`do not allow nullable type`); else if (metadata.any === true) insert(`do not allow any type`); return errors; };