dryvPacks

@dryv/class-validatorv0.7.0

class-validator DTO classes with field limits, formats and invariants.

class-validator DTOsValidationInjectPublished 7 days ago2 downloads

dryv pack add @dryv/class-validator

Generates one DTO class per schema, validated with class-validator and nested with class-transformer.

IRGenerated
optional / nullable@IsOptional() / @ValidateIf(o => o.x !== null)
string, number (integer), boolean@IsString(), @IsNumber() (@IsInt()), @IsBoolean()
string formats@IsUUID, @IsEmail, @IsUrl, @IsFQDN, @IsIP, @IsHexColor, @IsBase64, @IsJWT, @IsSemVer, @IsLocale, @IsISO31661Alpha2, @IsISO4217CurrencyCode, @IsJSON, … Loose phone, strict e164, slug and ulid use @Matches. Text-like formats (markdown, html, paths, …) stay @IsString()
length, pattern match@Length / @MinLength / @MaxLength, @Matches(new RegExp(...))
range, multiple of@Min / @Max, @IsDivisibleBy
enums@IsEnum(Enum) with the generated enum
temporal@IsISO8601() (strict for dates)
nested schemas, arrays@ValidateNested() + @Type(() => XDto); @IsArray(), @ArrayMinSize / @ArrayMaxSize, element rules with { each: true }
schema invariants@Satisfies<XDto>((object) => ..., { message }) on the first field the rule reads; opaque rules are noted in the class comment

Pattern starts, ends, allow and deny aren't rendered yet.

Provides: schema.validation and schema.types (the DTO classes) and property.enum.types (enums).

Test: bun scripts/test-pack.ts inject/validation/class-validator renders tests/fixture (a registration request), typechecks it, and runs validate() on valid and invalid samples, including the passwords-match invariant.

Use it

The executable Usage fixture shows the current source collection, destination and pack activation for this pack.

Slots

  • provides schema.validation
  • provides schema.types
  • provides property.enum.types

Generated tests

  • examples/<name>.dto.example.ts: the contract shape of each schema; provides schema.examples for other packs' tests.
  • testing/<name>.dto.spec.ts (Vitest): the DTO accepts its contract example, rejects unknown properties and rejects every single-constraint violation (missing, null, wrong type, length, range, format, pattern, enum, array bounds, nested).

Layout. DTOs, examples and specs are written under <group>/schema/dto/ (examples/, testing/), with shared validators and test helpers under common/schema/dto/.

Tests. Examples, specs and the shared example helper are written to the test output slot, so a project can keep tests outside its source folder. Map it with destinations.test.