question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Class To JSON Schema Conversion

See original GitHub issue

I am trying to convert my DTO class (Typescript) to JSON schema:

import { IsNumber, IsString } from 'class-validator';
import { classToPlain } from 'class-transformer';

export class TodoDTO {
    @IsNumber()
    id?: number;

    @IsString()
    name?: string;

    @IsString()
    description?: string;
}

let todo = classToPlain(TodoDTO);

console.log('todo=>', todo);

I tried to use two packages class-transformer and class-validator to transform and validate TodoDTO class.

In console it gives output as todo=> [Function: TodoDTO]

Expected output:

"TodoDTO": {
   "type": "object",
   "properties": {
       "id": { "type": "number" },
       "name": { "type": "string" },
       "description": { "type": "string" }
    },
   "required": [ "id", "name", "description" ]
}

I am trying to use TodoDTO class as a json-schema in fastify-typescript.

Any suggestions are welcome.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kamleshkatparacommented, Nov 9, 2021

@L2jLiga

Okay, i think i got it now it worked like this:

    @GET({
        url: '/count',
        options: {
            schema: {
                tags: ['Todos'],
                description: 'Returns count of todos',
                querystring: { $ref: 'TodoCountFilterDTO#' },
                response: { 200: TodosCountSchema, 401: { description: 'Unauthorized Access', type: 'object' }, 400: {}, 404: {}, 500: {} },
            } as FastifySchema
        }
    })

Thank you so much 👍🏻

0reactions
kamleshkatparacommented, Nov 9, 2021

Hi,

Now ids (definition ids) are dynamic but still can’t use it

image

image

image

image

In swagger it shows like params:

image

I want to it to be like querystring:

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Free Online JSON to JSON Schema Converter
Free Online JSON to JSON Schema Converter. Uses the sample JSON document to infer a JSON schema. Download Liquid Studio Access the online...
Read more >
Generate JSON schema from Java class - Stack Overflow
Use JJschema. It can generate draft 4 compliant JSON schemas. Refer this post http://wilddiary.com/generate ...
Read more >
Best JSON to Jsonschema Converter - JSON Formatter
JSON to Jsonschema Online with https and easiest way to convert JSON to Jsonschema. Save online and Share.
Read more >
jsonschema2pojo
Generate Plain Old Java Objects from JSON or JSON-Schema.
Read more >
Convert JSON to JSON Schema Online - ConvertSimple.com
Use this JSON to JSON Schema converter tool by pasting or uploading JSON in the left box below. Results will appear in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found