Merge branch 'master' into 4.2

master
Evgeny Poberezkin 2016-07-22 15:00:44 +01:00
commit d64b84f5ff
No known key found for this signature in database
GPG Key ID: 016D62451CED9D8E
4 changed files with 218 additions and 10 deletions

View File

@ -269,10 +269,10 @@ it.util.toHash(['a', 'b', 'c']) // { a: true, b: true, c: true }
Converts the string that is the key/index to access the property/item to the JavaScript syntax to access the property (either "." notation or "[...]" notation).
```javascript
it.util.toHash('a') // ".a"
it.util.toHash('1') // "['1']"
it.util.toHash("a'b") // "['a\\'b']"
it.util.toHash(1) // "[1]"
it.util.getProperty('a') // ".a"
it.util.getProperty('1') // "['1']"
it.util.getProperty("a'b") // "['a\\'b']"
it.util.getProperty(1) // "[1]"
```
@ -304,7 +304,7 @@ it.util.toQuotedString("a'b") // "'a\\'b'"
Returns the validation-time expression to safely access data based on the passed [relative json pointer](https://tools.ietf.org/html/draft-luff-relative-json-pointer-00) (See [examples](https://gist.github.com/geraintluff/5911303)).
```javascript
it.getData('2/test/1', it.dataLevel, it.dataPathArr)
it.util.getData('2/test/1', it.dataLevel, it.dataPathArr)
// The result depends on the current level
// if it.dataLevel is 3 the result is "data1 && data1.test && data1.test[1]"
```

View File

@ -147,6 +147,8 @@ Ajv is tested with these browsers:
[![Sauce Test Status](https://saucelabs.com/browser-matrix/epoberezkin.svg)](https://saucelabs.com/u/epoberezkin)
__Please note__: some frameworks, e.g. Dojo, may redifine global require in such way that is not compatible with CommonJS module format. In such case Ajv bundle has to be loaded before the framework and then you can use global Ajv (see issue #234).
## Command line interface
@ -225,10 +227,10 @@ This schema requires that the value in property `smaller` is less or equal than
var schema = {
"properties": {
"smaller": {
"type": number,
"type": "number",
"maximum": { "$data": "1/larger" }
},
"larger": { "type": number }
"larger": { "type": "number" }
}
};

View File

@ -141,7 +141,7 @@ function compile(schema, root, localRefs, baseId) {
refCode = 'refVal[' + refIndex + ']';
return resolvedRef(_refVal, refCode);
}
if (!isRoot) {
if (!isRoot && root.refs) {
var rootRefId = root.refs[ref];
if (rootRefId !== undefined) {
_refVal = root.refVal[rootRefId];

View File

@ -186,7 +186,7 @@ describe('issue #181, custom keyword is not validated in allErrors mode if there
describe('issue #210, mutual recursive $refs that are schema fragments', function() {
it('should compile and validate schema when one ref is fragment', function() {
var ajv = new Ajv();
var ajv = new Ajv;
ajv.addSchema({
"id" : "foo",
@ -220,7 +220,7 @@ describe('issue #210, mutual recursive $refs that are schema fragments', functio
});
it('should compile and validate schema when both refs are fragments', function() {
var ajv = new Ajv();
var ajv = new Ajv;
ajv.addSchema({
"id" : "foo",
@ -257,3 +257,209 @@ describe('issue #210, mutual recursive $refs that are schema fragments', functio
validate({ baz: { quux: { baz: "foo" } } }) .should.equal(false);
});
});
describe('issue #240, mutually recursive fragment refs reference a common schema', function() {
var apiSchema = {
$schema: 'http://json-schema.org/draft-04/schema#',
id: 'schema://api.schema#',
resource: {
id: '#resource',
properties: {
id: { type: 'string' }
}
},
resourceIdentifier: {
id: '#resource_identifier',
properties: {
id: { type: 'string' },
type: { type: 'string' }
}
}
};
var domainSchema = {
$schema: 'http://json-schema.org/draft-04/schema#',
id: 'schema://domain.schema#',
properties: {
data: {
oneOf: [
{ $ref: 'schema://library.schema#resource_identifier' },
{ $ref: 'schema://catalog_item.schema#resource_identifier' },
]
}
}
};
it('should compile and validate schema when one ref is fragment', function() {
var ajv = new Ajv;
var librarySchema = {
$schema: 'http://json-schema.org/draft-04/schema#',
id: 'schema://library.schema#',
properties: {
name: { type: 'string' },
links: {
properties: {
catalogItems: {
type: 'array',
items: { $ref: 'schema://catalog_item_resource_identifier.schema#' }
}
}
}
},
definitions: {
resource_identifier: {
id: '#resource_identifier',
allOf: [
{
properties: {
type: {
type: 'string',
'enum': ['Library']
}
}
},
{ $ref: 'schema://api.schema#resource_identifier' }
]
}
}
};
var catalogItemSchema = {
$schema: 'http://json-schema.org/draft-04/schema#',
id: 'schema://catalog_item.schema#',
properties: {
name: { type: 'string' },
links: {
properties: {
library: { $ref: 'schema://library.schema#resource_identifier' }
}
}
},
definitions: {
resource_identifier: {
id: '#resource_identifier',
allOf: [
{
properties: {
type: {
type: 'string',
'enum': ['CatalogItem']
}
}
},
{ $ref: 'schema://api.schema#resource_identifier' }
]
}
}
};
var catalogItemResourceIdentifierSchema = {
$schema: 'http://json-schema.org/draft-04/schema#',
id: 'schema://catalog_item_resource_identifier.schema#',
allOf: [
{
properties: {
type: {
type: 'string',
enum: ['CatalogItem']
}
}
},
{
$ref: 'schema://api.schema#resource_identifier'
}
]
}
ajv.addSchema(librarySchema);
ajv.addSchema(catalogItemSchema);
ajv.addSchema(catalogItemResourceIdentifierSchema)
ajv.addSchema(apiSchema);
var validate = ajv.compile(domainSchema);
testSchema(validate);
});
it('should compile and validate schema when both refs are fragments', function() {
var ajv = new Ajv;
var librarySchema = {
$schema: 'http://json-schema.org/draft-04/schema#',
id: 'schema://library.schema#',
properties: {
name: { type: 'string' },
links: {
properties: {
catalogItems: {
type: 'array',
items: { $ref: 'schema://catalog_item.schema#resource_identifier' }
}
}
}
},
definitions: {
resource_identifier: {
id: '#resource_identifier',
allOf: [
{
properties: {
type: {
type: 'string',
'enum': ['Library']
}
}
},
{ $ref: 'schema://api.schema#resource_identifier' }
]
}
}
};
var catalogItemSchema = {
$schema: 'http://json-schema.org/draft-04/schema#',
id: 'schema://catalog_item.schema#',
properties: {
name: { type: 'string' },
links: {
properties: {
library: { $ref: 'schema://library.schema#resource_identifier' }
}
}
},
definitions: {
resource_identifier: {
id: '#resource_identifier',
allOf: [
{
properties: {
type: {
type: 'string',
'enum': ['CatalogItem']
}
}
},
{ $ref: 'schema://api.schema#resource_identifier' }
]
}
}
};
ajv.addSchema(librarySchema);
ajv.addSchema(catalogItemSchema);
ajv.addSchema(apiSchema);
var validate = ajv.compile(domainSchema);
testSchema(validate);
});
function testSchema(validate) {
validate({ data: { type: 'Library', id: '123' } }) .should.equal(true);
validate({ data: { type: 'Library', id: 123 } }) .should.equal(false);
validate({ data: { type: 'CatalogItem', id: '123' } }) .should.equal(true);
validate({ data: { type: 'CatalogItem', id: 123 } }) .should.equal(false);
validate({ data: { type: 'Foo', id: '123' } }) .should.equal(false);
}
});