Skip to content

Commit

Permalink
Merge branch 'GPII-3904'
Browse files Browse the repository at this point in the history
* GPII-3904:
  GPII-3904: Added tests for and standardised handling of deep $schema values.
  Updated forward-facing version following 2.0.0 release.
  • Loading branch information
amb26 committed May 9, 2019
2 parents d181a1a + 2932d0a commit 01ac990
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gpii-json-schema",
"version": "2.0.0",
"version": "2.0.1",
"description": "Support validation of JSON within the Fluid and GPII ecosystems.",
"main": "index.js",
"scripts": {
Expand All @@ -21,7 +21,7 @@
"gpii-binder": "1.0.5",
"gpii-express": "1.0.15",
"gpii-handlebars": "1.1.3",
"infusion": "3.0.0-dev.20181010T171529Z.aeaa6e324.FLUID-6148",
"infusion": "3.0.0-dev.20190507T155813Z.4781871fd.FLUID-6148",
"kettle": "1.10.1"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/js/common/gss-metaschema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var fluid = fluid || {};
gpii.schema.metaSchema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "gss-v7-full#",
"title": "Fluid Schema-Lite syntax.",
"title": "GPII Schema Syntax.",
"definitions": {
"schemaArray": {
"type": "array",
Expand All @@ -21,7 +21,7 @@ var fluid = fluid || {};
},
"messageKey": {
"type": "string",
"pattern": "^[a-zA-Z0-9-_]+"
"pattern": "^[a-zA-Z0-9-_\.]+"
},
"messageKeyMap": {
"type": "object",
Expand Down
13 changes: 9 additions & 4 deletions src/js/common/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,15 @@ var fluid = fluid || {};
// We have to validate against a transformed copy of the original rawSchema so that AJV can enforce our
// required fields, which it would otherwise ignore.
var rawSchema = gpii.schema.gssToJsonSchema(gssSchema);
var validator = ajv.compile(rawSchema);
validator(toValidate);
try {
var validator = ajv.compile(rawSchema);
validator(toValidate);

return gpii.schema.validator.standardiseAjvErrors(gssSchema, validator.errors);
return gpii.schema.validator.standardiseAjvErrors(gssSchema, validator.errors);
}
catch (error) {
return { isError: true, message: "Error compiling GSS Schema.", errors: ajv.errors};
}
}
};

Expand Down Expand Up @@ -519,7 +524,7 @@ var fluid = fluid || {};
}

// If the GSS segment is an object, filter out our distinct keys such as `required` and `errors`.
var filteredSegment = Array.isArray(gssSegment) ? gssSegment : fluid.filterKeys(gssSegment, ["required", "hint", "errors", "enumLabels"], true);
var filteredSegment = Array.isArray(gssSegment) ? gssSegment : fluid.filterKeys(gssSegment, ["required", "hint", "errors", "enumLabels", "$schema"], true);
fluid.each(filteredSegment, function (value, key) {
// Preserve the value, making sure to give each nested object a chance to pull up its own list of required fields.
if (typeof value === "object") {
Expand Down
30 changes: 30 additions & 0 deletions tests/js/common/metaschema-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,35 @@ var jqUnit = jqUnit || {};
});

jqUnit.assertFalse("Additional properties not found in the GSS schema should be disallowed.", additionalPropertiesAllowed);

var nestedSchemasAllowed = ajv.validateSchema({
"$schema": "gss-v7-full#",
properties: {
named: {
"anyOf": [
{ $schema: "gss-v7-full#", type: "string" }
],
"allOf": [
{ $schema: "gss-v7-full#", maxLength: 3},
{ $schema: "gss-v7-full#", minLength: 1}
],
"oneOf": [
{ $schema: "gss-v7-full#", format: "email" },
{ $schema: "gss-v7-full#", format: "uri" }
]
}
},
additionalProperties: {
type: "object",
properties: {
settings: {
$schema: "gss-v7-full#",
type: "string"
}
}
}
});

jqUnit.assertTrue("Nested $schema elements should be handled correctly.", nestedSchemasAllowed);
});
})(fluid, Ajv, jqUnit);
49 changes: 49 additions & 0 deletions tests/js/common/validator-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,55 @@ var jqUnit = jqUnit || {};
"message": "custom-generic-error"
}]
}
},
schemaInData: {
message: "We should be able to validate schemas nested within a block of data.",
toValidate: {
name: "my custom data structure",
schema: {
$schema: "gss-v7-full#",
type: "object",
properties: {
colour: {
enum: ["#ff0000", "#0000ff", "#00ff00"],
enumLabels: ["Red", "Blue", "Green"]
}
}
}
},
schema: {
$schema: "gss-v7-full#",
properties: {
name: {
type: "string",
required: true
},
schema: {
$ref: "gss-v7-full#",
required: true
}
}
},
expected: {
isValid: true
}
},
nestedDollarSchema: {
message: "We should be able to handle nested $schema elements.",
schema: {
"$schema": "gss-v7-full#",
"type": "object",
"additionalProperties": {
"$schema": "gss-v7-full#",
"type": "string"
}
},
toValidate: {
"foo": "bar"
},
expected: {
isValid: true
}
}
};
fluid.each(testDefs, function (testDef) {
Expand Down

0 comments on commit 01ac990

Please sign in to comment.