garycourt/JSV — Issue #34: Extending schemas in multiple parent schemas
Author: pospi
Created: 2012-03-07 06:38:49+00:00
State: open

Hey there

This issue is related to [#37](https://github.com/garycourt/JSV/issues/32) - I think the issue is valid, but my reasoning as to why is not.

`additionalProperties` & `additionalItems` sealing the schema is fine, as I was extending from a base schema without these restrictions (all schemas specifying no additions were childmost ones in the hierarchy, which seems to work perfectly). The example I gave in issue 37 will actually work no problem.

The problem with child schema attributes all being merged into the same parent seems to arise when a base schema is extended multiple times within different parent schemas. Note that none of the schemas I'm testing with use additionalProperties/Items anymore.

Complete code to reproduce follows:

```
var schema1 = {
"id"	: "http://example.com/schemas/extendTest",
"$schema"	: "http://json-schema.org/draft-03/schema",
"description"	: "Testing extending the same schema multiple times",

"type" : "object",
"properties" : {
"set1" : {
"type" : "array",
"items" : {
"extends" : {"$ref" : "http://example.com/subRecord"},
"properties" : {
"newVar1" : {
"type" : "string",
"required" : true
}
}
}
},
"set2" : {
"type" : "array",
"items" : {
"extends" : {"$ref" : "http://example.com/subRecord"},
"properties" : {
"newVar2" : {
"type" : "string",
"required" : true
}
}
}
}
}
};

var schema2 = {
"id"	: "http://example.com/schemas/extendTest2",
"$schema"	: "http://json-schema.org/draft-03/schema",
"description"	: "Testing extending the same schema multiple times",

"type" : "object",
"properties" : {
"setA" : {
"type" : "array",
"items" : {
"extends" : {"$ref" : "http://example.com/subRecord"},
"properties" : {
"newVarA" : {
"type" : "string",
"required" : true
}
}
}
},
"setB" : {
"type" : "array",
"items" : {
"extends" : {"$ref" : "http://example.com/subRecord"},
"properties" : {
"newVarB" : {
"type" : "string",
"required" : true
}
}
}
}
}
};

var subschema = {
"id" : "http://example.com/subRecord",
"$schema"	: "http://json-schema.org/draft-03/schema",
"type" : "object",
"properties" : {
"someVar" : {
"type" : "string"
}
}
};

var Validator = JSV.createEnvironment('json-schema-draft-03');

Validator.createSchema(subschema, undefined, subschema.id);
Validator.createSchema(schema1, undefined, schema1.id);
Validator.createSchema(schema2, undefined, schema2.id);

console.log(Validator.validate({

set1 : [{
newVar1 : 'assigned',
someVar : 'assigned'
}]

}, Validator.findSchema('http://example.com/schemas/extendTest')));

console.log(JSchema.Validator.validate({

setA : [{
newVarA : 'assigned',
someVar : 'assigned'
}]

}, Validator.findSchema('http://example.com/schemas/extendTest2')));
```

Note that the first instance passes validation fine, but the second seems to inherit all properties from the first schema's extensions into its own and gives required property errors. If you reverse the order of schema registration, the errors will be in the first schema instead of the second.