Coder Social home page Coder Social logo

Comments (3)

gempain avatar gempain commented on June 19, 2024 1

Nevermind, just missed it in the docs:

{
    classTransformerMetadataStorage: defaultMetadataStorage,
}

from class-validator-jsonschema.

feimosi avatar feimosi commented on June 19, 2024

I'm having a similar issue. Even though I set up metadata storage it still returns "$ref": "#/definitions/Child" instead of "$ref": "#/components/schemas/Child".

Here's the code:

import { validationMetadatasToSchemas } from 'class-validator-jsonschema';
import { defaultMetadataStorage } from 'class-transformer/storage';

const schemas = validationMetadatasToSchemas({
  classTransformerMetadataStorage: defaultMetadataStorage,
});
import { ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';

class Child {
  name: string;
}

class BadRequestValidationError {
  @ValidateNested({each: true})
  @Type(() => Child)
  children!: Child[];
  @ValidateNested()
  @Type(() => Child)
  child!: Child;
}

The above definitions generated the following BadRequestValidationError schema:

{
  "properties": {
    "children": {
      "items": {
        "$ref": "#/definitions/Child"
      },
      "type": "array"
    },
    "child": {
      "$ref": "#/definitions/Child"
    }
  },
  "type": "object",
  "required": [
    "children",
    "child"
  ]
}

Logging defaultMetadataStorage content gives me the following output:

MetadataStorage {
  _typeMetadatas: Map(1) {
    [Function: BadRequestValidationError] => Map(2) { 'children' => [TypeMetadata], 'child' => [TypeMetadata] }
  },
  _transformMetadatas: Map(0) {},
  _exposeMetadatas: Map(0) {},
  _excludeMetadatas: Map(0) {},
  _ancestorsMap: Map(0) {}
}

Maybe there has been some breaking changes to class-transformer? Am I missing something?
@epiphone any ideas?

from class-validator-jsonschema.

epiphone avatar epiphone commented on June 19, 2024

Couple of things:

  1. Child class must have at least one class-validator decorator for it to get registered in class-transformer's defaultMetadataStorage
  2. refPointerPrefix property must be defined if you want to use reference paths of #/components/schemas instead of the default #/definitions https://github.com/epiphone/routing-controllers-openapi#validation-classes

I've updated the sample project to demonstrate nested validation, please check out epiphone/routing-controllers-openapi@bea6f3d.

from class-validator-jsonschema.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.