ThomasAribart / json-schema-to-ts

Infer TS types from JSON schemas 📝
MIT License
1.44k stars 31 forks source link

AJV format support ? #89

Closed olivierto closed 2 years ago

olivierto commented 2 years ago

Hey there! First of all Whow 🤯 So great utility plugin ! really powerfull and easy to use! you are typescript ninjas ! Great job !

Here is my issue. I'm using AJV format like this

import addFormat from 'ajv-formats'
import {FromSchema} from 'json-schema-to-ts';

export const validateParameters = (
  parametersData: any,
  schema: any
) => {
  const ajv = new Ajv({
    allErrors: true,
    removeAdditional: 'all',
    coerceTypes: true,
    unicodeRegExp: false,
  });
  addFormat(ajv);
  ...

Here is my schema code:

{
  type: 'object',
  properties: {
 supplierInvoiceFile: {
      type: 'object',
      properties: {
        type: {type: 'string'},
        filename: {type: 'string'},
        contentType: {type: 'string'},
        content: {type: 'binary', contentMediaType: 'application/pdf'},
      },
    },
}
}

and here is the calling code :

const body = parse(event, true) as FromSchema<typeof schema>;  //error 
/**
* Type '{ readonly type: "object"; readonly properties: { readonly isTest: { readonly type: "boolean"; readonly default: false; }; 
*  readonly supplierInvoiceFile: { readonly type: "object"; readonly properties: { readonly type: { readonly type: "string"; }; readonly 
*  filename: { ...; }; readonly contentType: { ...; }; readonly...' does not satisfy the constraint 'JSONSchema'.ts(2344)
**/

As soon as the property content is equal to "binary" it seems that the FromSchema is broken.

Is this a normal behavior as the support for binary type is not provided ? Or am I doing something wrong ?

Thanks for your answer !

olivierto commented 2 years ago

Sorry for the timeloss... I figured out by re reading the json schema docs The proper syntax is :

supplierInvoiceFile: {
      type: 'object',
      properties: {
        type: {type: 'string'},
        filename: {type: 'string'},
        contentType: {type: 'string'},
        content: {type: 'string', contentEncoding:'binary', contentMediaType: 'application/pdf'},
      },
    },