You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
QuickType output -- It is related to an earlier report I made #2556
Context (Environment, Version, Language)
Input Format: JSON Schema formatted as YAML
Output Language: JSON Schema formatted as JSON, as well as typescript-zod
CLI, npm, or app.quicktype.io: npm package CLI
Version: 23.0.115
Description
Obviously when one adds additional attributes to a schema entry, those attributes should be reflected in the generated code.
Because they're not reflected in generated code, I'd have to re-edit that code to have the attributes.
Input Data
Sample JSON Schema in YAML format
$schema: "http://json-schema.org/draft-06/schema#"$id: "./bad-schema.schema.json"title: "Station"description: "A product in the catalog"type: "object"properties:
simpleString:
description: | Simple string has no problemtype: stringpattern:
description: | String with a pattern, the pattern goes throughtype: stringpattern: '^[0-9][0-9][0-9][0-9][0-9]$'zipCode:
description: | String with min/masLength, hence with numerical attributes, and a pattern, only the pattern goes through.type: stringmaxLength: 5minLength: 5pattern: '^[0-9][0-9][0-9][0-9][0-9]$'minMaxLength:
type: stringmaxLength: 5minLength: 5simpleNumber:
description: | Simple number no problemtype: numberpercent:
description: | Attributes on number do not go throughtype: numberminimum: 0maximum: 1percentage:
description: | Attributes on number do not go throughtype: numberminimum: 0maximum: 100enumSchema:
type: stringenum:
- enum1
- enum2
- enum3
This demonstrates a few variants of the issue so you can see different angles.
Expected Behaviour / Output
The pattern I've found so far is that attributes with string values are generated into the output, while those with numerical output are not.
My first stage test is conversion of the YAML-formatted schema to JSON format because of course most software will want to see JSON formatted JSON schema's. But, the YAML format is easier to read and edit while being functionally equivalent.
In generated TypeScript the validation code simply does not reflect any attribute. And, in the generated type definition there are no generated TSDOC annotations.
In the generated TypeScript-Zod validator, no attributes are generated.
Also, I hand-edited a copy of the JSON formatted schema to have the desired attributes. No code generated from that schema has the desired attribute values.
This generates the following - which has no sign of the attributes.
// To parse this data://// import { Convert, BadSchemaEditedTs } from "./file";//// const badSchemaEditedTs = Convert.toBadSchemaEditedTs(json);//// These functions will throw an error if the JSON doesn't// match the expected interface, even if the JSON is valid./** * A product in the catalog */exportinterfaceBadSchemaEditedTs{minMaxLength?: string;/** * String with a pattern, * the pattern goes through */pattern?: string;/** * Attributes on number do not * go through */percent?: number;/** * Attributes on number do not * go through */percentage?: number;/** * Simple number no problem */simpleNumber?: number;/** * Simple string has no problem */simpleString?: string;/** * String with min/masLength, hence * with numerical attributes, and * a pattern, only the pattern goes * through. */zipCode?: string;[property: string]: any;}// Converts JSON strings to/from your types// and asserts the results of JSON.parse at runtimeexportclassConvert{publicstatictoBadSchemaEditedTs(json: string): BadSchemaEditedTs{returncast(JSON.parse(json),r("BadSchemaEditedTs"));}publicstaticbadSchemaEditedTsToJson(value: BadSchemaEditedTs): string{returnJSON.stringify(uncast(value,r("BadSchemaEditedTs")),null,2);}}functioninvalidValue(typ: any,val: any,key: any,parent: any=''): never{constprettyTyp=prettyTypeName(typ);constparentText=parent ? ` on ${parent}` : '';constkeyText=key ? ` for key "${key}"` : '';throwError(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`);}functionprettyTypeName(typ: any): string{if(Array.isArray(typ)){if(typ.length===2&&typ[0]===undefined){return`an optional ${prettyTypeName(typ[1])}`;}else{return`one of [${typ.map(a=>{returnprettyTypeName(a);}).join(", ")}]`;}}elseif(typeoftyp==="object"&&typ.literal!==undefined){returntyp.literal;}else{returntypeoftyp;}}functionjsonToJSProps(typ: any): any{if(typ.jsonToJS===undefined){constmap: any={};typ.props.forEach((p: any)=>map[p.json]={key: p.js,typ: p.typ});typ.jsonToJS=map;}returntyp.jsonToJS;}functionjsToJSONProps(typ: any): any{if(typ.jsToJSON===undefined){constmap: any={};typ.props.forEach((p: any)=>map[p.js]={key: p.json,typ: p.typ});typ.jsToJSON=map;}returntyp.jsToJSON;}functiontransform(val: any,typ: any,getProps: any,key: any='',parent: any=''): any{functiontransformPrimitive(typ: string,val: any): any{if(typeoftyp===typeofval)returnval;returninvalidValue(typ,val,key,parent);}functiontransformUnion(typs: any[],val: any): any{// val must validate against one typ in typsconstl=typs.length;for(leti=0;i<l;i++){consttyp=typs[i];try{returntransform(val,typ,getProps);}catch(_){}}returninvalidValue(typs,val,key,parent);}functiontransformEnum(cases: string[],val: any): any{if(cases.indexOf(val)!==-1)returnval;returninvalidValue(cases.map(a=>{returnl(a);}),val,key,parent);}functiontransformArray(typ: any,val: any): any{// val must be an array with no invalid elementsif(!Array.isArray(val))returninvalidValue(l("array"),val,key,parent);returnval.map(el=>transform(el,typ,getProps));}functiontransformDate(val: any): any{if(val===null){returnnull;}constd=newDate(val);if(isNaN(d.valueOf())){returninvalidValue(l("Date"),val,key,parent);}returnd;}functiontransformObject(props: {[k: string]: any},additional: any,val: any): any{if(val===null||typeofval!=="object"||Array.isArray(val)){returninvalidValue(l(ref||"object"),val,key,parent);}constresult: any={};Object.getOwnPropertyNames(props).forEach(key=>{constprop=props[key];constv=Object.prototype.hasOwnProperty.call(val,key) ? val[key] : undefined;result[prop.key]=transform(v,prop.typ,getProps,key,ref);});Object.getOwnPropertyNames(val).forEach(key=>{if(!Object.prototype.hasOwnProperty.call(props,key)){result[key]=transform(val[key],additional,getProps,key,ref);}});returnresult;}if(typ==="any")returnval;if(typ===null){if(val===null)returnval;returninvalidValue(typ,val,key,parent);}if(typ===false)returninvalidValue(typ,val,key,parent);letref: any=undefined;while(typeoftyp==="object"&&typ.ref!==undefined){ref=typ.ref;typ=typeMap[typ.ref];}if(Array.isArray(typ))returntransformEnum(typ,val);if(typeoftyp==="object"){returntyp.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers,val)
: typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems,val)
: typ.hasOwnProperty("props") ? transformObject(getProps(typ),typ.additional,val)
: invalidValue(typ,val,key,parent);}// Numbers can be parsed by Date but shouldn't be.if(typ===Date&&typeofval!=="number")returntransformDate(val);returntransformPrimitive(typ,val);}functioncast<T>(val: any,typ: any): T{returntransform(val,typ,jsonToJSProps);}functionuncast<T>(val: T,typ: any): any{returntransform(val,typ,jsToJSONProps);}functionl(typ: any){return{literal: typ};}functiona(typ: any){return{arrayItems: typ};}functionu(...typs: any[]){return{unionMembers: typs};}functiono(props: any[],additional: any){return{ props, additional };}functionm(additional: any){return{props: [], additional };}functionr(name: string){return{ref: name};}consttypeMap: any={"BadSchemaEditedTs": o([{json: "minMaxLength",js: "minMaxLength",typ: u(undefined,"")},{json: "pattern",js: "pattern",typ: u(undefined,"")},{json: "percent",js: "percent",typ: u(undefined,3.14)},{json: "percentage",js: "percentage",typ: u(undefined,3.14)},{json: "simpleNumber",js: "simpleNumber",typ: u(undefined,3.14)},{json: "simpleString",js: "simpleString",typ: u(undefined,"")},{json: "zipCode",js: "zipCode",typ: u(undefined,"")},],"any"),};
Desires
To use this with AJV for example the attributes must be in the JSON-schema.
For generated Zod validation code to be useful, it must have the attributes as part of the Zod schema.
Several tools exist for JavaScript/TypeScript that can perform validation derived from the JSDOC/TSDOC annotations. Hence it would be useful to output those annotations.
The generated TypeScript code looks like it wants to do validation but isn't there.
The text was updated successfully, but these errors were encountered:
Issue Type
QuickType output -- It is related to an earlier report I made #2556
Context (Environment, Version, Language)
Input Format: JSON Schema formatted as YAML
Output Language: JSON Schema formatted as JSON, as well as typescript-zod
CLI, npm, or app.quicktype.io: npm package CLI
Version: 23.0.115
Description
Obviously when one adds additional attributes to a schema entry, those attributes should be reflected in the generated code.
Because they're not reflected in generated code, I'd have to re-edit that code to have the attributes.
Input Data
Sample JSON Schema in YAML format
This demonstrates a few variants of the issue so you can see different angles.
Expected Behaviour / Output
The pattern I've found so far is that attributes with string values are generated into the output, while those with numerical output are not.
My first stage test is conversion of the YAML-formatted schema to JSON format because of course most software will want to see JSON formatted JSON schema's. But, the YAML format is easier to read and edit while being functionally equivalent.
In generated TypeScript the validation code simply does not reflect any attribute. And, in the generated type definition there are no generated TSDOC annotations.
In the generated TypeScript-Zod validator, no attributes are generated.
Also, I hand-edited a copy of the JSON formatted schema to have the desired attributes. No code generated from that schema has the desired attribute values.
Steps to Reproduce
Using the above YAML-formatted schema ...
This produces:
Generate Zod validators:
This generates:
No attributes make it to the output.
Then, to disprove the idea that the problem is due to the YAML formatted schema, I hand edited the JSON formatted generated schema:
This made no difference in other generated code.
As for the generated TypeScript code, shouldn't the attributes show up there?
This generates the following - which has no sign of the attributes.
Desires
To use this with AJV for example the attributes must be in the JSON-schema.
For generated Zod validation code to be useful, it must have the attributes as part of the Zod schema.
Several tools exist for JavaScript/TypeScript that can perform validation derived from the JSDOC/TSDOC annotations. Hence it would be useful to output those annotations.
The generated TypeScript code looks like it wants to do validation but isn't there.
The text was updated successfully, but these errors were encountered: