Comments (2)
Thanks @pvh for you workaround. I found it fixes the issue partially only. It is because the library is adding required: true
also into all object properties subschemes and into all array items subchemes.
Example:
Original JSON:
{
"welcomeTo": "JSON document",
"arra": [1,3,5]
}
Generated scheme:
{
"type": "object",
"properties": {
"welcomeTo": {
"type": "string",
"required": true // Error stays here
},
"arra": {
"type": "array",
"items": {
"type": "integer",
"required": true // Error stays here
},
"required": true // Error stays here
}
},
"required": [ // Error fixed here by @pvh workaround
"welcomeTo",
"arra"
],
from to-json-schema.
Sharing my own workaround function. You can call it on generated schema to fix required
property everywhere.
/**
* Fixes the schema generator's bug.
* @see https://github.com/ruzicka/to-json-schema/issues/12
*/
function fixSchemaBug(schema: any): void {
if (typeof schema?.required === 'boolean') {
// Fix 'object' type
if (schema.type === 'object' && typeof schema.properties === 'object') {
// Fix the incorrect `required` property.
schema.required = Object.keys(schema.properties);
// Apply recursively for all object's properties.
Object.values(schema.properties).forEach((propertySubSchema) => fixSchemaBug(propertySubSchema));
} else {
// No `required` needed.
// TODO: Can this situation (`type==='object'`, but no `properties` defined) happend, or is it not possible in JSON schema?
delete schema.required;
}
// Fix 'array' type
if (schema.type === 'array' && typeof schema.items === 'object') {
// Apply recursively for array items type.
fixSchemaBug(schema.items);
// Optionally we can force array to be non-empty.
// /*enable if needed*/ schema.minItems = 1;
}
}
}
from to-json-schema.
Related Issues (10)
- anyOf spec
- typescript support improvements
- It is there a CLI? HOT 1
- Show nested object
- some string are inferred as "format":"style"
- Fail to create schema of array of strings HOT 1
- Remove `mocha` as production dependency
- Fails to find the schema for an array of arrays when one array is empty HOT 1
- API flexibility suggestions
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 to-json-schema.