ajv-validator / ajv-validator/ajv-keywords

Containing $ref in select statement throws error unless fully qualified.

未关闭
#42 9 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
limitation
主要语言
TypeScript
星标
257
派生
51
PR 合并指标
30 天内没有已合并 PR

描述

Example code below throws the following error when it attempts to compile selectSchema2:

```
/Users/sirockin/source/f2/f2-schema/node_modules/ajv/lib/dotjs/ref.js:61
throw new it.MissingRefError(it.baseId, $schema, $message);
^
Error: can't resolve reference defs.json#/definitions/int from id #
```
Example code:
```
var Ajv = require('ajv');

var schema = {
"$id": "http://example.com/schemas/schema.json",
"type": "object",
"properties": {
"foo": { "$ref": "defs.json#/definitions/int" },
"bar": { "$ref": "defs.json#/definitions/str" }
}
};

var selectSchema1={
"$id": "http://example.com/schemas/select1.json",
type: "object",
required: ['kind'],
properties: {
kind: { type: 'string' }
},
select: { $data: '0/kind' },
selectCases: {
foo: {
required: ['foo'],
properties: {
kind: {},
foo: { type: 'string' }
},
additionalProperties: false
},
bar: {
required: ['bar'],
properties: {
kind: {},
bar: { type: 'number' }
},
additionalProperties: false
}
},
selectDefault: {
propertyNames: {
not: { enum: ['foo', 'bar'] }
}
}
};

var selectSchema2={
"$id": "http://example.com/schemas/select2.json",
type: "object",
required: ['kind'],
properties: {
kind: { type: 'string' }
},
select: { $data: '0/kind' },
selectCases: {
foo: {
required: ['foo'],
properties: {
kind: {},
foo: { "$ref": "defs.json#/definitions/int" }
},
additionalProperties: false
},
bar: {
required: ['bar'],
properties: {
kind: {},
bar: { type: 'number' }
},
additionalProperties: false
}
},
selectDefault: {
propertyNames: {
not: { enum: ['foo', 'bar'] }
}
}
};

var defsSchema = {
"$id": "http://example.com/schemas/defs.json",
"definitions": {
"int": { "type": "integer" },
"str": { "type": "string" }
}
};

// Validate against simple schema
var ajv = new Ajv({schemas: [schema, defsSchema]});
var validate = ajv.getSchema('http://example.com/schemas/schema.json');

var result = validate({ foo:2});
console.log("Simple schema validation result: " + result);

// Validate against select schema
ajv = new Ajv({schemas: [selectSchema1, defsSchema],$data:true});
require('ajv-keywords')(ajv, 'select');
validate = ajv.getSchema('http://example.com/schemas/select1.json');

var result = validate({ kind: 'foo', foo:"blah" });
console.log("Select schema validation result: " + result);

// Validate against select schema with refs - throws an error
ajv = new Ajv({schemas: [selectSchema2, defsSchema],$data:true});
require('ajv-keywords')(ajv,'select');
validate = ajv.getSchema('http://example.com/schemas/select2.json'); // Error thrown here
var result = validate({ kind: 'foo', foo: 'any' });
console.log("Select with refs schema validation result: " + result);

```

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。