What's the correct use of a resolver?
See original GitHub issueHi, I want to have a IMC field automatically computed with the value of two other fields POIDS and TAILLE, and I want this field to be read only.
For that sake I use the following (simplified) schema:
{
"$schema": "http://json-schema.org/draft-03/schema#",
"type": "object",
"properties": {
"TAILLE_CLI":{
"title": "Taille",
"type": "number",
"description": "Entrez la taille en mètre. par exemple: 1.75"
},
"POIDS_CLI":{
"title": "Poids",
"type": "number",
"description": "Entrez le poids en Kg. par exemple: 75.5"
},
"IMC_CLI":{
"title": "IMC",
"readOnly": true,
"type": "number",
"description": "Indice de masse corporelle = poids/taille²",
"dependsOn": ["POIDS_CLI", "TAILLE_CLI"]
}
}
}
and the following (simplified) resolver:
// return the resolved schema given the specific business rule
resolver : function(names, data, cb) {
var schemas = [];
var sch = new Object();
var current = currentSchema.properties;
switch(names[0]) {
case "$.IMC_CLI":
sch = JSON.parse(JSON.stringify(current.IMC_CLI));
if(data.POIDS_CLI !== undefined && data.TAILLE_CLI !== undefined) {
sch.enum = [data.POIDS_CLI / (data.TAILLE_CLI * data.TAILLE_CLI),""];
sch.readOnly = true;
}
break;
}
schemas[names] = sch;
cb(schemas);
}
If I entirely clone ‘current.IMC_CLI’ into ‘sch’ with
sch = JSON.parse(JSON.stringify(current.IMC_CLI));
I get the following error:
Uncaught TypeError: Cannot read property ‘type’ of undefined
at render (brutusin-json-forms.js:1210)
at renderers.object (brutusin-json-forms.js:598)
at render (brutusin-json-forms.js:1224)
at Object.obj.render (brutusin-json-forms.js:831)
at client.html:25
If I comment out
sch = JSON.parse(JSON.stringify(current.IMC_CLI));
My IMC_CLI field is never visible
The only way I found to have my IMC_CLI field behaving correctly is to copy member by member ‘current’ into ‘sch’ like this:
case "$.IMC_CLI":
sch.type = current.IMC_CLI.type;
sch.title = current.IMC_CLI.title;
sch.description = current.IMC_CLI.description;
sch.required = current.IMC_CLI.required;
sch.readOnly = current.IMC_CLI.readOnly;
if(data.POIDS_CLI !== undefined && data.TAILLE_CLI !== undefined) {
sch.required = true;
sch.enum = [data.POIDS_CLI / (data.TAILLE_CLI * data.TAILLE_CLI),""];
sch.readOnly = true;
}
break;
Is there a simpler way to return a fully filled object from resolver without having to copy each member, which is error prone?
Note: when cloning the object with the JSON stringify/parse method I see a strange behaviour in the last for loop of populateSchemaMap() function. After a few series of calls, the ‘name’ parameter of the function becomes “$.IMC_CLI,$.IMC_CLI” instead of “$.IMC_CLI”. I assume this is due to the returned value of my resolver.
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (4 by maintainers)
Top GitHub Comments
be aware that by using the simple assignment your are modifying the original schema, since you have two references to the same instance
Ok much simpler test. Updating simple example.