Closed david-pa closed 1 year ago
Hi David.
$defs
has been introduced with draft-07. Before that, you had to use property definitions
for this.
Either use the contructor Draft07
or change $defs
to definitions
.
Does this solve your issue?
Does the $defs
need to be changed to definitions
within the schema, the .compileSchema()
call, or both? Is Draft07
compatible with a Draft04
schema, or would there be some data misinterpretation?
Using
const newSchema = new Draft07(mySchema);
const compiledSchema = newSchema.compileSchema({ $ref: "/$defs/Input" });
const inputSchema = compiledSchema.getRef();
It seems like using Draft07
as the constructor in the above code ended up returning inputSchema
as:
{
$defs: { Input: {...}},
$ref: '$defs/Input'
}
This still isn't quite what I was expecting per the documentation, though.
Hi David.
json-schema specifications evolve over time and contain breaking changes in between. To learn about json-schema, please refer to the json-schema website. They also have a very helpful tutorial page and a reference to all the schema specifications.
I suggest you stick to the latest schema version, for json-schema-library this is Draft07:
you get an empty array, because getTemplate only creates a minimal data that validates. This also protects against the creation of inifinite long lists, etc:
import { Draft07 } from "json-schema-library";
const draft = new Draft07(mySchema);
draft.getTemplate();
// {"inputs":[]}
you can define the number of items to be created:
mySchema.properties.inputs.minLength = 1;
draft.setSchema(mySchema);
draft.getTemplate();
// {"inputs":[{"name":"","value":""}]}
or you can pass a number of items:
draft.setSchema(mySchema);
draft.getTemplate({ inputs: [{}, {}] });
// {"inputs":[{"name":"","value":""},{"name":"","value":""}]}
you can also pass a subschema to getTemplate directly:
draft.getTemplate(null, mySchema.$defs.Input);
// { name:"first", value:"" }
and you can use getTemplate to merge input data
draft.getTemplate({ "inputs": [{ name: "first" }]});
// { "inputs": [{ name:"first", value:"" }]}
you can also get the schema via a json-pointer:
const InputSchema = draft.getSchema().getRef("#/$defs/Input");
This should help you to get started.
Notes
/..
or a #/..
. A valid pointer to root data is ''
or '#'
This clears up the issues I was having, thank you!
Thank you for the confirmation.
Cheers
@sagold I do have one last thing that has popped up! I am having an issue where I have something defined in $defs
such that I can get back the schema via
exampleSchema = mySchema.getSchema().getRef('#/$defs/Example');
but if I try to put that into a call like so:
mySchema.getTemplate(null, exampleSchema);
all I get back is undefined
. I'm not sure what the issue is, but I can't even get a template using mySchema.getTemplate(null, baseSchema.$defs.Example);
, which has worked so far for other definitions.
Is there a common reason why something like this may happen?
EDIT: The issue was a missing type declaration of object
in the schema file! Adding that fixed it.
Hi David.
Can you post your schema? Maybe there is an issue with the schema or the support of it in getTemplate. I cannot tell without the actual schema.
I have a schema I'm working with that is something like the following:
When I use:
I get back a JSON object that looks something like:
What I need to do now is grab a template of the
Input
definition referenced in the schema's$defs
section, but I'm not quite sure how to do that. I thought that something likewould work, but that didn't yield any results. All that happened was that
compiledSchema
andinputSchema
were the exact same sets of data, no schema to be found. What I'm expecting to get back is something like:But I'd also need to know how to get something like
as
value
in this definition isn't required, but I may have access to that data depending on the context and will want to fill it in.