Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression bug validationMetadatasToSchemas v5.0.0 #100

Open
hxtree opened this issue Jan 23, 2023 · 7 comments
Open

Regression bug validationMetadatasToSchemas v5.0.0 #100

hxtree opened this issue Jan 23, 2023 · 7 comments

Comments

@hxtree
Copy link

hxtree commented Jan 23, 2023

There appears to be a regression bug. The instructions provided in the readme work on 3.1.1 but do not work on version 5.0.0

// eslint-disable-next-line max-classes-per-file
import { Type } from 'class-transformer';
import { IsString, ValidateNested } from 'class-validator';
import { validationMetadatasToSchemas } from 'class-validator-jsonschema';

const { defaultMetadataStorage } = require('class-transformer/cjs/storage');

class BlogPost {
  @IsString()
  public words: string;
}

class User {
  @ValidateNested({ each: true })
  @Type(() => BlogPost)
  public blogPosts: BlogPost[];
}

const schemas = validationMetadatasToSchemas({
  classTransformerMetadataStorage: defaultMetadataStorage,
});
    TypeError: Cannot read properties of undefined (reading 'map')

      17 | }
      18 |
    > 19 | const schemas = validationMetadatasToSchemas({
         |                                             ^
      20 |   classTransformerMetadataStorage: defaultMetadataStorage,
      21 | });
      22 |

      at populateMetadatasWithConstraints (../../common/temp/node_modules/.pnpm/[email protected]_e6kgdsnyya5caxg3ysdyxrqm7a/node_modules/class-validator-jsonschema/src/index.ts:146:20)
      at getMetadatasFromStorage (../../common/temp/node_modules/.pnpm/[email protected]_e6kgdsnyya5caxg3ysdyxrqm7a/node_modules/class-validator-jsonschema/src/index.ts:137:23)
      at validationMetadatasToSchemas (../../common/temp/node_modules/.pnpm/[email protected]_e6kgdsnyya5caxg3ysdyxrqm7a/node_modules/class-validator-jsonschema/src/index.ts:29:21)
      at Object.<anonymous> (src/__tests__/te.test.ts:19:45)     

When using version 5.0.0 the only way to call validationMetadatasToSchemas appears to be without any "()" or "({}".

@mcankudis
Copy link

Got a similar error on version 3.1.1 after upgrading the class-validator from 0.13.2 to 0.14. Upgraded class-validator-jsonschema to 5.0.0 and things started working again. If you haven't done so yet, try upgrading the class-validator, maybe that helps.

The error for reference:

TypeError: metadatas.map is not a function
    at populateMetadatasWithConstraints (<my_path>/node_modules/class-validator-jsonschema/src/index.ts:141:20)
    at getMetadatasFromStorage (<my_path>/node_modules/class-validator-jsonschema/src/index.ts:132:10)
    at validationMetadatasToSchemas (<my_path>/node_modules/class-validator-jsonschema/src/index.ts:26:21)

@Luccazx12
Copy link

same here (with class-validator 0.14.0)

 return metadatas.map((meta) => {
app_1             |                      ^
app_1             | 
app_1             | TypeError: metadatas.map is not a function
app_1             |     at populateMetadatasWithConstraints (.../node_modules/class-validator-jsonschema/build/index.js:60:22)
app_1             |     at getMetadatasFromStorage (/(.../node_modules/class-validator-jsonschema/build/index.js:56:12)
app_1             |     at validationMetadatasToSchemas ((.../node_modules/class-validator-jsonschema/build/index.js:15:23

@Luccazx12
Copy link

Luccazx12 commented Feb 2, 2023

I was able to solve it as follows:

import * as oa from "openapi3-ts";
import { getMetadataArgsStorage, RoutingControllersOptions } from "routing-controllers";
import { routingControllersToSpec } from "routing-controllers-openapi";
import { validationMetadatasToSchemas } from "class-validator-jsonschema";

const metadataSchemas = validationMetadatasToSchemas();

const storage = getMetadataArgsStorage();
    return routingControllersToSpec(storage, routingControllersOptions, {
      components: {
        schemas: metadataSchemas as Record<string, oa.SchemaObject | oa.ReferenceObject>
      }
});

Library versions

    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "class-validator-jsonschema": "^5.0.0",
    "routing-controllers": "^0.10.1",
    "routing-controllers-openapi": "^4.0.0",

@afraz-khan
Copy link

same here (with class-validator 0.14.0)

 return metadatas.map((meta) => {
app_1             |                      ^
app_1             | 
app_1             | TypeError: metadatas.map is not a function
app_1             |     at populateMetadatasWithConstraints (.../node_modules/class-validator-jsonschema/build/index.js:60:22)
app_1             |     at getMetadatasFromStorage (/(.../node_modules/class-validator-jsonschema/build/index.js:56:12)
app_1             |     at validationMetadatasToSchemas ((.../node_modules/class-validator-jsonschema/build/index.js:15:23

@mcankudis I upgraded class-validator-jsonschema to 5.0.0 but still finding this issue.

@gregkysenko
Copy link

Hello! Any updates for this issue? I am trying to integrate class-validator-jsonschema and It seem I can`t work with nested objects.

@Luccazx12
Copy link

Hello! Any updates for this issue? I am trying to integrate class-validator-jsonschema and It seem I can`t work with nested objects.

For object arrays too

@osnipezzini
Copy link

anyone created some fork with this fix ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants