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
3.2 KBRaw ↗
yaml
version: dryv.pack/v1alpha1
key: inject.validation.class-validator
source: templates

info:
  title: class-validator DTOs
  version: 0.7.0
  description: Generates DTO classes with class-validator and class-transformer decorators for every schema.
  purpose: validation
  summary: class-validator DTO classes with field limits, formats and invariants.
  languages: [typescript]
  frameworks: [class-validator, class-transformer]
  tags: [dto, validation]

plugins:
  - name: jinja
    extensions: [.j2]

# Enums and named property types come from the bound types pack (inject/types/typescript).
needs:
  property.enum.types: { required: true }
  property.types: { required: true }

selections:
  # Every schema gets a DTO, so request and response shapes are both covered.
  schemas:
    subject: schema
    scope: each

templates:
  dto:
    $ref: '#/selections/schemas'
    output:
      name: $(subject.name.kebab)
      path:
        - $(group.name.kebab)
      symbol: $(subject.name.pascal)Dto
    imports:
      - $ref: '#/needs/property.enum.types'
      - $ref: '#/needs/property.types'
      - $ref: '#/templates/dto'

  # <group>/dto/examples/<name>.dto.example.ts: the contract shape of each schema, for generated tests.
  example:
    $ref: '#/selections/schemas'
    output:
      destination: test
      name: $(subject.name.kebab)
      path:
        - $(group.name.kebab)
      symbol: $(subject.name.camel)Example
    imports:
      - $ref: '#/needs/property.enum.types'
      - $ref: '#/templates/example'

  # <group>/dto/testing/<name>.dto.spec.ts (Vitest): the DTO accepts its example and rejects each violation.
  spec:
    $ref: '#/selections/schemas'
    output:
      destination: test
      name: $(subject.name.kebab)
      path:
        - $(group.name.kebab)
    imports:
      - $ref: '#/templates/dto'
        members: true
      - $ref: '#/templates/example'
        members: true

  # The shared helper the generated tests use; written with the tests, not the source.
  testing-support:
    output:
      destination: test

provides:
  schema.validation:
    $ref: '#/templates/dto'
  schema.types:
    $ref: '#/templates/dto'
  schema.examples:
    $ref: '#/templates/example'

# shared:js-package-manager:start
choose:
  js.package_manager: { options: [pnpm, npm, bun, yarn] }
# shared:js-package-manager:end

# Libraries the generated code imports, and the formatter its format action runs.
dependencies:
  runtime:
    class-transformer: ^0.5
    class-validator: ^0.14
    reflect-metadata: ^0.2
  dev:
    prettier: "*"
    randexp: ^0.5
    vitest: ^3

# shared:js-prettier-actions:start
actions:
  format:
    stage: files
    extensions: [.ts]
    commands:
      - run: [(pnpm|yarn), dlx, [email protected], --ignore-path, .prettier-none, --write, $(files)]
      - run: [npm, exec, --yes, --package, [email protected], --, prettier, --ignore-path, .prettier-none, --write, $(files)]
      - run: [bun, x, [email protected], --ignore-path, .prettier-none, --write, $(files)]
  install:
    stage: final
    dependencies:
      args:
        - dev: { (pnpm|yarn): [-D], bun: [--dev], npm: [--save-dev] }
        - { pinned: "$(name)@$(version)", any: "$(name)" }
    commands:
      - run: [(pnpm|bun|yarn), add]
      - run: [npm, install]
# shared:js-prettier-actions:end