@dryv/class-validatorv0.7.0
class-validator DTO classes with field limits, formats and invariants.
dryv pack add @dryv/class-validator{#- This schema's own DTO class and example (members of this template). -#}
{%- macro own(template) -%}
{%- for dependency in file.dependencies -%}
{%- if dependency.target.id == schema.id and dependency.template == template -%}{{ dependency.symbol }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
{%- macro own_path(template) -%}
{%- for dependency in file.dependencies -%}
{%- if dependency.target.id == schema.id and dependency.template == template -%}{{ (dependency.path.prefixed or ('./' ~ dependency.path.relative)) }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
import "reflect-metadata";
import { plainToInstance } from "class-transformer";
import { validate } from "class-validator";
import { describe, expect, it } from "vitest";
import { {{ own('dto') }} } from "{{ own_path('dto') }}";
import { {{ own('example') }} } from "{{ own_path('example') }}";
/** Managed by Dryv: {{ own('dto') }} enforces exactly what the {{ schema.name.pascal }} contract says. */
async function errorsFor(value: Record<string, unknown>): Promise<string[]> {
const errors = await validate(plainToInstance({{ own('dto') }}, value), {
whitelist: true,
forbidNonWhitelisted: true,
forbidUnknownValues: false,
});
return errors.map((error) => error.property);
}
const cases = {{ own('example') }}.violations().map((item) => [item.field, item.reason, item.value] as const);
describe("{{ own('dto') }} contract", () => {
it("accepts the contract example", async () => {
expect(await errorsFor({{ own('example') }}.valid())).toEqual([]);
});
it("rejects unknown properties", async () => {
expect(await errorsFor({ ...{{ own('example') }}.valid(), unexpectedField: 1 })).toContain(
"unexpectedField",
);
});
if (cases.length > 0) {
it.each(cases)("rejects %s when %s", async (field, _reason, value) => {
expect(await errorsFor(value)).toContain(field);
});
}
});