How to get jsonschema for only one class ?
See original GitHub issueHello,
In your exemple you give:
import { IsOptional, IsString, MaxLength } from 'class-validator'
import { validationMetadatasToSchemas } from 'class-validator-jsonschema'
class BlogPost {
@IsString() id: string
@IsOptional()
@MaxLength(20, { each: true })
tags: string[]
}
const schemas = validationMetadatasToSchemas()
console.log(schemas)
Unfortunatly I have too much classes using class-validator and I only want some. Is there a way to only get for given ones ?
Something like this:
import { IsOptional, IsString, MaxLength } from 'class-validator'
import { validationMetadatasToSchemas } from 'class-validator-jsonschema'
class BlogPost {
@IsString() id: string
@IsOptional()
@MaxLength(20, { each: true })
tags: string[]
}
const schema = validationClassToSchema(BlogPost) // or validationClassToSchemas([BlogPost])
console.log(schema)
Tried to create my own MetadataStorage
with only the classes I want to be in but I don’t find any exemples on how to achieve that. Did you have ?
Actually I do:
const schemasToGet = [BlogPost.name];
const configSchema = validationMetadatasToSchemas();
for (const name of schemasToGet) {
this._configSchema[name] = configSchema[name];
}
Thanks,
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:10 (1 by maintainers)
Top Results From Across the Web
Getting Started Step-By-Step | JSON Schema
To start a schema definition, let's begin with a basic JSON schema. We start with four properties called keywords which are expressed as...
Read more >How do I require one field or another or (one of two others) ...
If one of the properties is * submitted with a truthy string value, then the other will * not be required to have...
Read more >Introduction to JSON Schema in Java
A beginner's look at JSON Schema: a declarative language for validating the format and structure of a JSON Object.
Read more >JSON Schema Serializer and Deserializer
Plug the KafkaJsonSchemaSerializer into KafkaProducer to send messages of JSON Schema type to Kafka. Assuming you have a Java class that is decorated...
Read more >How to Validate Your JSON Using JSON Schema
A sample schema, like what we'd get from json.load() > ... In some cases, the value can be of only a specific type,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just stumbled across this issue, my case is that my schema names across project are not by any means unique, and there is few completely separate domains.
Btw. sorry for spam / long samples.
My solution is to recursivelly inline schema objects instead of using refs with custom
NESTED_VALIDATION
constraint converter. But it could be easily adapted to dynamic definition.So class
Options
withNestedOptions
Produces huge but valid schema
// EDIT - solution with definitions object
@scorsi
Just in case you’re still wondering about this, I managed to get it to work by making a new file that will hold my schema and doing something like this:
Creates a new class that extends my initial class but omits the _id property, you can also pass an empty array and I believe it still works. This provides basically the same class to the validationMetadatasToSchemas and allows me to get a result.