typia
Version:
Superfast runtime validators with only one line
99 lines (92 loc) • 2.78 kB
text/typescript
import ts from "typescript";
import { StatementFactory } from "../../factories/StatementFactory";
import { TypeFactory } from "../../factories/TypeFactory";
import { IProgrammerProps } from "../../transformers/IProgrammerProps";
import { ITypiaContext } from "../../transformers/ITypiaContext";
import { FeatureProgrammer } from "../FeatureProgrammer";
import { ValidateProgrammer } from "../ValidateProgrammer";
import { FunctionProgrammer } from "../helpers/FunctionProgrammer";
import { HttpQueryProgrammer } from "./HttpQueryProgrammer";
export namespace HttpValidateQueryProgrammer {
export interface IProps extends IProgrammerProps {
allowOptional?: boolean;
}
export const decompose = (props: {
context: ITypiaContext;
modulo: ts.LeftHandSideExpression;
functor: FunctionProgrammer;
type: ts.Type;
name: string | undefined;
allowOptional: boolean;
}): FeatureProgrammer.IDecomposed => {
const validate = ValidateProgrammer.decompose({
...props,
context: {
...props.context,
options: {
...props.context.options,
functional: false,
numeric: false,
},
},
config: {
equals: false,
},
});
const decode = HttpQueryProgrammer.decompose(props);
return {
functions: {
...validate.functions,
...decode.functions,
},
statements: [
...validate.statements,
StatementFactory.constant({
name: "__validate",
value: validate.arrow,
}),
StatementFactory.constant({
name: "__decode",
value: decode.arrow,
}),
],
arrow: ts.factory.createArrowFunction(
undefined,
undefined,
decode.arrow.parameters,
props.context.importer.type({
file: "typia",
name: "IValidation",
arguments: [decode.arrow.type ?? TypeFactory.keyword("any")],
}),
undefined,
ts.factory.createCallExpression(
ts.factory.createIdentifier("__validate"),
undefined,
[
ts.factory.createCallExpression(
ts.factory.createIdentifier("__decode"),
undefined,
[ts.factory.createIdentifier("input")],
),
],
),
),
};
};
export const write = (props: IProps): ts.CallExpression => {
const functor: FunctionProgrammer = new FunctionProgrammer(
props.modulo.getText(),
);
const result: FeatureProgrammer.IDecomposed = decompose({
...props,
functor,
allowOptional: !!props.allowOptional,
});
return FeatureProgrammer.writeDecomposed({
modulo: props.modulo,
functor,
result,
});
};
}