ruzicka / to-json-schema Goto Github PK
View Code? Open in Web Editor NEWConverts JS objects to JSON Schema
License: MIT License
Converts JS objects to JSON Schema
License: MIT License
Seems like you accidentally put mocha in dependencies
instead of devDependencies
.
Hi, interesting project.
I was wondering if you had given any thought about how to do anyOf support in maybe a custom function
strings which are not "style" are inferred as styles.
for example, given this code:
import toJsonSchema from 'to-json-schema'
const json = {
'notAStyle': '10:00',
}
console.log(toJsonSchema(json))
the following json schema is generated:
{
"type": "object",
"properties": {
"notAStyle": {
"type": "string",
"format": "style"
}
}
}
I think the regex here can be improved to avoid this mistakes: https://github.com/ruzicka/to-json-schema/blob/master/src/jsonSchemaHelpers.js#L74
I would love to upload PR for this one if possible
Is there any way to show the nested objects which I do not know the schema of?
Im getting:
{
type: 'object',
properties: {
bpi: { type: 'object', properties: [Object] },
disclaimer: { type: 'string' },
time: { type: 'object', properties: [Object] }
}
}
where I want:
{
type: 'object',
properties: {
bpi: { type: 'object', properties: { valueIdontknowA: { type: 'string' } , valueIdontknowB: { type: 'string' } } },
disclaimer: { type: 'string' },
time: { type: 'object', properties:
{ valueA: { type: 'string' } , valueB: { type: 'string' } }
}
}
}
Conversion attempt of ["1", "two", "3", "four"] to schema throws exception with message "Failed to load schema: Cannot read properties of null (reading 'type')".
The cause is that mergeSchemaObjs returns null because list items schemas restored as {"type": "string", "format": "utc-millisec"} for "1" and "3", and {"type": "string"} for the rest.
Hi there -- not sure if this project is very active but figured I'd post this for the benefit of anyone else wondering.
I believe the required: true
notation used to indicate properties are required is not valid JSON-schema syntax in any draft. I tested this by pasting the generated output of to-json-schema
into a JSON schema validator and checking it against the existing JSON schema drafts!
That said, there appears to be an easy fix, which would be to use the example you define farther down:
const options = {
objects: {
postProcessFnc: (schema, obj, defaultFnc) => ({...defaultFnc(schema, obj), required: Object.getOwnPropertyNames(obj)})
}
};
Thanks for this library @ruzicka, it's nicely engineered!
See test case here: https://github.com/ruzicka/to-json-schema/compare/master...forivall:forivall%2Ftest-array-of-arrays?diff=unified
If i add a string to the second array (like [['foo'], ['bar']]
), then the test case passes. As is, the generated schema is:
{
"type": "array",
"items": {
"type": "array"
}
}
options.object
, options.string
, options.postProcessFnc
are not flexible enough to handle complex cases, such as patch/merge some schema fileds on toJsonSchema
processing.
For details:
Lack of array options.
Lack of options.preProcessFnc
Lack of object.keys and array.index hook
What I really need is the node path during toJsonSchema
iteration process, something like:
options.postProcessFnc(nodePath = ['a', 1, 'b'], value = 'myValue', schema) {
// patch on result schema
if (nodePath.join('.') === 'a.1.b') {
schema.example = 'my code';
schema.anything = 'xxxx';
}
return schema;
}
But at this point, documented API seems not working for this case (especially array index), any idea or workaround would be also appreciated.
Hi,
I'd like to define a JSON file such as
{
"user" : "sample"
}
And save it as user.json
and then run something such as to-json-schema users.json userschema.json
in order to create a schema file for the users.json
file
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.