tdegrunt/jsonschema — pull request #37 by janmentzel (2013-04-17 20:03:12+00:00)
Title: extends having a $ref to schema uri removed other sibling attributes

I'm relatively new to the json-schema topic. So please let me know if I'm wrong.

## Expected Behaviour

For example we want an instance to have a field `amount`.

```
{
"description" : "Instance Schema",
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema",
"properties": {
"amount": {
"required": true,
"extends" : {"$ref": "types.json#/positive_int"}
}
}
}
```

The constraints of `amount` will be used in several other schemas. So we extracted its definition as `positive_int` to an other schema file called `types.json`:

```
{
"description" : "our custom types",
"type":"object",
"positive_int": {
"description" : "positive integer > 0",
"type" : "integer",
"minimum": 1,
}
}
```

When validating an instance against the schema the validator should resolve the `extends` and its `$ref` uri to:

```
{
"description" : "Instance Schema",
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema",
"properties": {
"amount": {
"required": true,
"description" : "Amount of the payment in cents, must be > 0",
"type" : "integer",
"minimum": 1,
}
}
}
```

The same should be expected with the maybe more comon usage of extends in our case

```
...
"amount": {
"required": true,
"extends" : "types.json#/positive_int"
}
...
```

## Unwanted Behaviour

Currently `Validator` did not resolve `$ref` uri before merging the contents of `extends` into its parent node.
Though the merge itself seems to be correct according [extends](http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.26).
Resulting in the following:

```
...
"amount": {
"required": true,
"$ref": "types.json#/positive_int"
}
...
```
the validator then continued with resolving `$ref` what lead to extinguishing all siblings (correct behaviour according [$ref](http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.28)).

```
...
"amount": {
"description" : "Amount of the payment in cents, must be > 0",
"type" : "integer",
"minimum": 1,
}
...
```
So the `required` attribute was unwantedly dropped resulting to `amount` beeing optional in our example.

If `extends` value is a plain uri string a `TypeError: Object.keys called on non-object` is thrown in `lib/validator.js:168`

```
{
"description" : "Instance Schema",
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema",
"properties": {
"amount": {
"required": true,
"extends" : "types.json#/positive_int"
}
}
}
```