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

classValidatorToJsonSchema - Get JSON Schema for a specific class #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jan-demsar
Copy link

Hello.

First of all, thank you for amazing package! It is very useful, but in my case I needed ability to extract JSON Schema for a specific class (with duplicate names).

The problem with validationMetadatasToSchemas is that it merges schemas with the same names.

So if I have 1 User class with name property and another with firstName, JSON Schema will include both (obviously I could use unique names, but for current project I cannot avoid duplicates).

As a workaround I created a separate function which solves the issue for me and might be useful for someone else as well.

Copy link
Owner

@epiphone epiphone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! This will totally be a useful addition. Left a few comments.

Also, does this work with nested validation? https://github.com/typestack/class-validator#validating-nested-objects. I have a feeling it might not because of the target metadata filtering in https://github.com/epiphone/class-validator-jsonschema/pull/48/files#diff-a2a171449d862fe29692ce031981047d7ab755ae7f84c707aef80701b3ea0c80R79.

@@ -12,7 +12,7 @@ module.exports = {
moduleDirectories: ['node_modules', 'src'],
moduleFileExtensions: ['ts', 'js', 'json'],
roots: ['<rootDir>/__tests__'],
testPathIgnorePatterns: ['/node_modules/'],
testPathIgnorePatterns: ['/node_modules/', '/__tests__/classes'],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary?

Comment on lines +4 to +8
export class User {
@MinLength(5)
@IsString()
name: string;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to use namespaces in this file to import two classes with the same name? Might be clearer than declaring one here and another in a separate file.

I mean something like

namespace Foo { export class User { } }
namespace Bar { export class User { } }

Comment on lines +79 to +82
const targetMetadatas = _(metadatas)
.filter(metadata => {
return metadata.target === target
}).value();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the native Array.filter here instead of lodash

/**
* Convert class-validator class into JSON Schema definition.
*/
export type ClassType<T> = new (...args: any[]) => T;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export type ClassType<T> = new (...args: any[]) => T;
export type ClassType = new (...args: any[]) => any;

We can omit the unused generic here and below I think.

Comment on lines +4 to +8
export class User {
@MinLength(5)
@IsString()
name: string;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also test that if the two User classes both have a property with the same name but different class-validator decorators, only the desired decorators get applied in the resulting schema?

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

Successfully merging this pull request may close these issues.

2 participants