Comments (6)
@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.
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.
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.
@darcyparker in repository are this file - schema.json; This can help?
from json-schema-to-typescript.
@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.
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)
- Support `prefixItems` for tuple validation HOT 2
- Feature Request: Generate Classes instead of interfaces HOT 2
- how to handle this error "This interface was referenced by ..." HOT 1
- `RangeError` for circular references to `oneOf` containing definition with the `$ref` HOT 3
- Crash for deprecated field missing description HOT 1
- A bit unclear about the purpose of `declareExternallyReferenced` and intention HOT 1
- Support ESM usage of module HOT 3
- Create `index.ts` when generating files from a directory HOT 1
- List on json-schema.org HOT 1
- Create Typscript schema with external references
- Convert empty object to Record<string,never> HOT 3
- Remove optional flag in case of `default` in schema. HOT 8
- `strictIndexSignatures` option is ignored for single pattern properties
- Package lists dev dependencies as dependencies HOT 4
- If `minProperties` is greater than or equal to the number of declared properties and `additionalProperties` is `false`, all properties should be required HOT 2
- properties + anyOf generates incorrect output
- `compile()` ignores `$refOptions.resolve` settings HOT 1
- What package manager does the repo use? HOT 6
- `SyntaxError` while building browser bundle HOT 2
- Add option to remove `[k: string]: unknown | undefined;` from generated types HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-schema-to-typescript.