Git Product home page Git Product logo

Comments (6)

darcyparker avatar darcyparker commented on May 22, 2024 1

@cawa-93 - I may not be understanding your problem...

I compiled the schema.json and get the following

  • It seems to agree with what I read in the schema.
  • Perhaps you are expecting it to parse some definitions from the other files?
    • If so, I think you need to create a schema that references these definitions.
    • My initial look at the JSON schemas is that they have many definitions but they aren't referenced outside of the definitions scope. (I think the definitions are meant to be referenced by other JSON schema.)
export interface Error {
  /**
   * Error name
   */
  name: string;
  /**
   * Error code
   */
  code: number;
  /**
   * Error description
   */
  description: string;
}
/**
 * Parameter type
 */
export type ParameterTypes = "array" | "boolean" | "integer" | "number" | "string";
export type ItemTypes = "boolean" | "integer" | "number" | "string";
export interface Parameter {
  /**
   * Parameter name
   */
  name: string;
  /**
   * Parameter type
   */
  type: ParameterTypes;
  items?: {
    type: ItemTypes;
    maximum?: number;
    minimum?: number;
  };
  maxItems?: number;
  minItems?: number;
  maximum?: number;
  minimum?: number;
  enum?: any[];
  default?: any;
  required?: boolean;
  maxLength?: number;
  minLength?: number;
  /**
   * Parameter description
   */
  description?: string;
}
export interface Method {
  /**
   * Method name
   */
  name: string;
  /**
   * Method description
   */
  description?: string;
  /**
   * Is it open method
   */
  open?: boolean;
  /**
   * Input parameters for method
   */
  parameters?: Parameter[];
  /**
   * References to response objects
   */
  responses?: Object;
  /**
   * Possible errors
   */
  errors?: Error[];
}
/**
 * API declaration
 */
export interface Ischema {
  /**
   * Possible errors
   */
  errors?: Error[];
  methods?: Method[];
}

from json-schema-to-typescript.

wiktor-k avatar wiktor-k commented on May 22, 2024 1

Sorry for reviving this old thread but I had the same need - to generate TypeScript interfaces for nested types inside definitions. Fortunately it's easy to do with compile and some glue code:

import { compile } from 'json-schema-to-typescript'

import { readFileSync } from 'fs';

const schema = JSON.parse(readFileSync('schema.json', 'utf8'));

for (const name in schema.definitions) {
    // name is the key in definitions map that is used as an interface name
    schema.definitions[name].title = name;
    console.log(compile(schema.definitions[name]));
}

Maybe the example above will be useful for someone.

from json-schema-to-typescript.

darcyparker avatar darcyparker commented on May 22, 2024

The referenced source JSON has definitions but no properties that reference these definitions. (See https://github.com/json-schema-org/json-schema-spec/blob/master/schema.json as an example of a schema with definitions that are referenced in the properties.) I suspect your objects.json is intended to be referenced by other schemas because it doesn't define properties for itself.

BTW: I get the same result as you and ajv (https://github.com/epoberezkin/ajv) compiles the following: (Notice the validator always returns true which agrees with export type Objects = any;

// ======================================================================
//  Generated JSON validate for: "objects" JSON object
// ======================================================================
//           JSON Schema Source: json-schema/data-api/v1/objects.json
//          sha1 of JSON Schema: 8eee6d2eed1a1f3ddaaaae2281b54d20da2a9e0b
//            Validate Compiler: ajv v4.7.4
// ======================================================================
/* jshint ignore: start */
/* eslint-disable */
module.export = function validate (data, dataPath, parentData, parentDataProperty, rootData) {
  'use strict';
  if (rootData === undefined) rootData = data;
  validate.errors = null;
  return true;
}

Note, I added my own custom header to ajv output. But I don't touch the compiled validator

from json-schema-to-typescript.

cawa-93 avatar cawa-93 commented on May 22, 2024

@darcyparker in repository are this file - schema.json; This can help?

from json-schema-to-typescript.

cawa-93 avatar cawa-93 commented on May 22, 2024

@darcyparker, you says:

I suspect your objects.json is intended to be referenced by other schemas because it doesn't define properties for itself.

I suggested that perhaps schema.json - this is it.
Unfortunately I am not a developer of the scheme. I just wanted to use it in his TypeScript

from json-schema-to-typescript.

darcyparker avatar darcyparker commented on May 22, 2024

Good luck with it. I can empathize with the process of transforming the JSON schemas to typescript interface definitions and validators.

I was thinking about your examples a little more. The compiler is doing its job of creating an interface definition in typescript for the JSON schema. But there could situations where it would useful to have interface definitions for JSON schema definitions that aren't actually used by the current JSON schema. Perhaps the compiler could pre-emptively create typescript interface definitions for each definition in the JSON schema definitions block. And then, as they are used, reference them... and if they aren't used (yet) you will have them for future use. I have no need for this yet... but it might be worth writing a ticket if/when you or someone else has the need.

from json-schema-to-typescript.

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.