Does this support $ref?
Hiya,
I have a schema with "$schema": "http://json-schema.org/draft-04/schema#", "title": "title", "type": "object", "item": { "type": "object", "properties": { "attribute": { "$ref": "#/definitions/schema2" } }, "required": [ "attribute_info" ] } .... something like that.
I kept the form simple with the "*" and a submit button. However the form doesn't render properly where the $ref is.
am I doing something wrong?
No. References are planned to be implemented, but it isn't yet.
Cool! Great to know :) :+1:
Would you happen to know when the implementation is planned to happen?
Hard to say right now. We might need it in the project we're working on at Textalk, in that case it will be implemented before november. Otherwise it is more uncertain.
If someone else wants to have a go at it, go ahead :) I guess it would be best to follow the reference while recursing into that level when expanding the form, keeping in mind that references could be cyclic. Probably finding a good JSON-Pointer parser...
darn. I hope that the project will need it :)
I tried the generator with a schema that has a nested object. didn't work either :[ I'm guessing this only works with flattened schemas?
@dkengaroo do you mean something like this? (That works fine for me)
The Schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["name"],
"properties": {
"name": {
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Person Name",
"description": "A person's name",
"type": "object",
"required": ["familyName", "givenName"],
"properties": {
"familyName": {
"type": "string", "maxLength": 30, "minLength": 1,
"title": "Surname"
},
"givenName": {
"type": "string", "maxLength": 20, "minLength": 1,
"title": "First Name"
},
"middleName": {
"type": "string", "maxLength": 20, "minLength": 1,
"title": "Middle Name"
}
}
}
}
}
Hiya,
the code you provided works for me too! However, when I do this:
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["name"],
"properties": {
"name": {
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Person Name",
"description": "A person's name",
"type": "object",
"properties": {
"groups":{
"type": "object",
"required": ["familyName", "givenName"],
"properties": {
"familyName": {
"type": "string", "maxLength": 30, "minLength": 1,
"title": "Surname"
},
"givenName": {
"type": "string", "maxLength": 20, "minLength": 1,
"title": "First Name"
},
"middleName": {
"type": "string", "maxLength": 20, "minLength": 1,
"title": "Middle Name"
}
}
}
}
}
}
It works as well. However the word "groups" only shows up as "g" in the form
Does it have issues because of something when it is traversing?
When I put your example in http://textalk.github.io/angular-schema-form/examples/bootstrap-example.html it works just fine, doesn't it? Are you using the most recent release v0.7.2? I think there was a problem with default title #67
Hey! it works when I get v0.7.2! :) should have done an update earlier...
$ref support would be a real :+1:
whoa! Nice! :+1:
pretty spiffy :)
Could this issue be re-opened, and re-labeled as an enhancement / improvement request? If the maintainers would be receptive to including it, we may be willing to code the improvement and/or have it done.
Hi, with regards to the implementation of that, it is quite complicated, if not impossible to solve in a completely general matter, as schemas aren't always exposed as files.
However, a midway is to not overthink it, but to leave some of that to the user. I think the python jsonschema library solves this quite nicely by letting the user implement a uri handler for each namespace. So when the validator encounters a $ref with an unknown namespace, it checks the if the resolver handles it, which in turn tries its handlers. And if any does, it returns the schema(a dict in python). In the case below, it is against a file, but it could of course be against an array, object properties or whatever.
As you see, the handler is very easy to implement, and the same should work in JavaScript.
def mbe_uri_handler(self, uri):
""" Handle the mbe:// references """
# Use urlparse to parse the file location from the URI
_file_location = self.json_schema_folder + urlparse(uri).netloc
return json.loads(open(_file_location, "r", encoding="utf-8").read())
def __init__(self)
.....
.....
_resolver = RefResolver(base_uri="",
handlers={"mbe": self.mbe_uri_handler}, referrer=None, cache_remote=True)
self.d4validator = Draft4Validator({}, resolver=_resolver)
So, for example, if I have schemas referencing other scemas, I use a specific namespace. The code below, for example, references another file, and is resolved by the uri handler:
"schemaId": {
"description": "The schema id of the node schema, a uuid.",
"$ref" :"mbe://custom.json#properties/uuid"
},
I could the declare another handler for another namespace, like "obj:", that references to schemas stored in an objects' properties(like a wannabe dictionary). And I can then, in a schema, use "$ref" :"obj://customer#properties/address_postal" and let the handler figure out that "customer" means the schema stored in $scope.object.customer or whatever.
Docs: https://python-jsonschema.readthedocs.org/en/latest/references/ Resolver code: https://github.com/Julian/jsonschema/blob/master/jsonschema/validators.py#L219
The same pattern could obviously then apply to the forms as $ref could be used there as well. It would be great to have inheritance there as well.
This is really important for me on a project I am using angular-schema-form for, I wish I had time to work on it myself. I just haven't found a good json resolver that can output a full schema with predefined base URI yet.
That said json-refs is working minus a base URI.
TV4 has JavaScript based code for handling it with a base URI defined, key code is in the function ValidatorContext.prototype.resolveRefs and just below it ValidatorContext.prototype.getSchema
But just does validation and wont output a compiled schema for use with angular-schema-form
Hope that helps someone :)
I would think that as long as angular-schema-form is provided a way to get its hands on the referenced JSON structure, finding something in said structure should be fairly trivial. The same method and $ref-syntax could be used to extend forms, I presume.
It is important for me as well to get this working, I do have some time to work on it and make a pull request, but would first like to have some kind of discussion on what would be an acceptable approach. I am also not completely familiar with the direction of the project; what would be a angular-schema-form-istic solution?
If there aren't any major objections I could write something that uses the TV4-solution that Anthropic suggested. I suppose that there could be some value in having at least similar interfaces?
@nicklasb check out #118 there seems to be a solution using json-refs, or at least people that have got it working. It feels like "resolving" all the refs in a schema before rendering it with angular schema form is the more flexible solution, but I'm open to changing my mind. Would that work for you?
I think this must be implemented inside schema form, and it must be done with infinite recursion in mind.
Consider this schema:
{
"$ref": "#/definitions/sub",
"definitions": {
"sub": {
"type": "object",
"properties": { "sub": {"$ref": "#/definitions/sub" } },
"additionalProperties": false
}
}
}
This will work in tv4, but it can't be expanded before usage. You can try it in http://json-schema-validator.herokuapp.com/ with data: { "sub": { "sub": { "sub": {} } } }
With this in mind, a form definition can't be generated from the schema without also considering the model; only if the sub-object has the key "sub", THEN the reference must be followed and generate a sub-form.
@fiddur yes that is certainly a show stopper for resolving all refs outside of angular schema form, but do we need to support this? Is there a real world example where infinitively recursive forms is useful?
Not generating a form for it though unless its in the model? what does that even mean? How should that be represented in the actual DOM/HTML? Currently an object as in your example is just a fieldset or a section (just a div) or not part of it at all if you point out the specific properties in your form definition.