ipetrovic11 / ts-transformer-json-schema Goto Github PK
View Code? Open in Web Editor NEWThis project forked from kimamula/ts-transformer-keys
A TypeScript custom transformer to obtain JSON schema from interface.
License: MIT License
This project forked from kimamula/ts-transformer-keys
A TypeScript custom transformer to obtain JSON schema from interface.
License: MIT License
I've created this project and I can't seem to get the transform working, I was hoping that you'd be able to take a quick look at it and let me know what I've done wrong please:
https://github.com/Anupheaus/ts-transform-test
Thank you.
interface IUser {
email: string;
password: string;
}
interface ICompany {
workspace: string;
}
interface IOnboardRequest {
user: IUser;
company: ICompany;
}
This is a weird one.
An example:
import { schema } from 'ts-transformer-json-schema';
enum MyEnum {
A = 'A',
B = 'B'
}
interface IContainsMyEnum {
enum?: MyEnum;
}
console.log(JSON.stringify(schema<IContainsMyEnum>(), null, 2));
When strict mode is disabled in tsconfig.json
(i.e. strict
property not set in compilerOptions
), the output is as expected:
{
"enum": {
"type": "enum",
"values": [
"A",
"B"
],
"optional": true
}
}
However, when strict mode is enabled (i.e. "strict": "true"
in compilerOptions
), the output is this:
{
"enum": [
{
"type": "MyEnum.A"
},
{
"type": "MyEnum.B"
},
{
"type": "forbidden"
}
]
}
fastest-validator
then complains that MyEnum.A
etc. are not valid types, which is correct.
It should be noted that this does not occur with non-optional enums, i.e. they generate the correct enum
type in the validation schema.
enum UserGroup {
Admin = 'admin',
Manager = 'manager',
Employee = 'employee'
}
enum UserConfirmationState {
Pending = 'pending',
Confirmed = 'confirmed'
}
interface IUserWithGroupsAndState {
groups: UserGroup[];
confirmation: UserConfirmationState;
}
Allow specific properties to be excluded from the generated schema.
Currently transformer doesn't solve:
https://www.typescriptlang.org/docs/handbook/interfaces.html#indexable-types
Example:
interface Something { [key: string]: string; }
I think the name of the project should be changed to ts-transformer-fastest-validator for clarity.
Having a nested interface e.g.
interface IInner {
a: string;
}
interface IOuter {
inner: IInner;
}
Calling schema<IOuter>
yields:
{
inner: { a: { type: "string" } }
}
When it should be (according to https://github.com/icebob/fastest-validator#object):
{
inner: {
type: "object",
props: {
a: { type: "number" }
}
}
interface IInner {
num: number;
str: string;
}
interface IOuter {
nested: IInner;
num: number;
}
Should generate the following output:
{
num: { type: 'number'},
nested: {
type: "object", props: {
num: { type: 'number'},
str: { type: 'string'}
},
}
However it currently generates the following:
{
type: "object",
props: {
num: { type: 'number'}
nested: {
num: { type: 'number'},
str: { type: 'string'}
},
}
}
Currently:
interface IOptional {
readonly optional?: string | number;
}
is resolved into:
{
type: 'object', props: {
optional: [{ type: "string" }, { type: "number" }]
}
}
which actually just ignores ?(question mark - optional).
fastest-validator doesn't have example how that should be handled, there are two options on my mind currently:
{
type: 'object', props: {
optional: [{ type: "string" }, { type: "number" },{type: "forbidden"}]
}
}
or
{
type: 'object', props: {
optional: {
optional: true, type: [{ type: "string" }, { type: "number" }]
}
}
}
interface IPrimitiveUnion {
x: number | string;
}
import type {FooBar} from 'foo-bar';
const schema = schema<FooBar>()
Always returns type any
interface Person {
name: string;
age: number;
phone?: string;
}
interface IStep1 {
step2: IStep2;
}
interface IStep2 {
step1: IStep1;
}
In this case there is infinite recursion which should be stopped.
Proposal for solution:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.