ajv-validator / ajv-validator/ajv-merge-patch

Cannot get it to work with subschemas

未关闭
#41 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
JavaScript
星标
47
派生
19
PR 合并指标
30 天内没有已合并 PR

描述

First of all: thank you for your work. I have a question that I can't figure out for the life of me. I wonder if you can figure out what I'm missing here?

I'm trying to patch a schema and then validate data against subschemas of the schema. It seems the patch is not applied to subschemas, but somehow applied to the root schema (without taking the source path defined in the patch into account).

```json
// fhir.schema.test.json
{
"$id": "http://hl7.org/fhir/json-schema/4.0#",
"discriminator": {
"propertyName": "resourceType",
"mapping": {
"Account": "#/definitions/Account",
"Patient": "#/definitions/Patient"
}
},
"oneOf": [
{
"$ref": "#/definitions/Account"
},
{
"$ref": "#/definitions/Patient"
}
],
"definitions": {
"Account": {
"properties": {
"resourceType": {
"const": "Account"
}
},
"additionalProperties": false,
"required": [
"resourceType"
]
},
"Patient": {
"properties": {
"resourceType": {
"const": "Patient"
}
},
"additionalProperties": false,
"required": [
"resourceType"
]
}
}
}
```

```javascript
const schemas = [
require('./fhir.schema.test.json'),
{
$patch: {
source: { $ref: '#/definitions/Patient' },
with: [
{ op: 'add', path: '/properties/q', value: { type: 'number' } },
{ op: 'add', path: '/required/-', value: 'q' }
]
}
}
];

const everything = Object.assign(...schemas);
ajv.addSchema(everything, 'key');

const rootSchema = ajv.getSchema(`key`);
const getSubSchema = definition => ajv.getSchema(`key#/definitions/${definition}`);
```

```javascript
// TESTS AND EXPECTATIONS

const tests = [
{ resourceType: 'Account' }, // Expect true (no q necessary in 'Account')
{ resourceType: 'Patient', q: 1 }, // Expect true
{ resourceType: 'Patient', q: 'abc' }, // Expect false (q is wrong type)
{ resourceType: 'Patient' }, // Expect false (missing q)
];

console.log('rootSchema', inspect(rootSchema.schema, { depth: null }));

tests.forEach(test => {
const subSchema = getSubSchema(test.resourceType);
console.log('test:', test);
console.log('rootSchema isValid:', rootSchema(test));
console.log('rootSchema errors:', rootSchema.errors);
console.log('subSchema:', inspect(subSchema.schema, { depth: null }));
console.log('subSchema isValid:', subSchema(test));
console.log('subSchema errors:', subSchema.errors);
});
```

output:
```sh
rootSchema {
'$id': 'http://hl7.org/fhir/json-schema/4.0#',
discriminator: {
propertyName: 'resourceType',
mapping: {
Account: '#/definitions/Account',
Patient: '#/definitions/Patient'
}
},
oneOf: [
{ '$ref': '#/definitions/Account' },
{ '$ref': '#/definitions/Patient' }
],
definitions: {
Account: {
properties: { resourceType: { const: 'Account' } },
additionalProperties: false,
required: [ 'resourceType' ]
},
Patient: {
properties: { resourceType: { const: 'Patient' } },
additionalProperties: false,
required: [ 'resourceType' ]
}
},
'$patch': {
source: { '$ref': '#/definitions/Patient' },
with: [
{ op: 'add', path: '/properties/q', value: { type: 'number' } },
{ op: 'add', path: '/required/-', value: 'q' }
]
}
}
```
```sh
test: { resourceType: 'Account' }
rootSchema isValid: false
rootSchema errors: [
{
keyword: 'const',
dataPath: '.resourceType',
schemaPath: '#/properties/resourceType/const',
params: { allowedValue: 'Patient' }, # <-- WHY? IT'S AN 'Account'?
message: 'should be equal to constant'
},
{
keyword: 'required',
dataPath: '',
schemaPath: '#/required',
params: { missingProperty: 'q' },
message: "should have required property 'q'" # <-- NO IT SHOULD NOT
},
{
keyword: '$patch',
dataPath: '',
schemaPath: '#/$patch',
params: { keyword: '$patch' },
message: 'should pass "$patch" keyword validation'
}
]
subSchema: {
properties: { resourceType: { const: 'Account' } },
additionalProperties: false,
required: [ 'resourceType' ]
}
subSchema isValid: true
subSchema errors: null
```
```sh
test: { resourceType: 'Patient', q: 1 }
rootSchema isValid: false
rootSchema errors: [
{
keyword: 'required',
dataPath: '',
schemaPath: '#/required',
params: { missingProperty: 'q' },
message: "should have required property 'q'" # <-- IT HAS PROPERTY q!
},
{
keyword: '$patch',
dataPath: '',
schemaPath: '#/$patch',
params: { keyword: '$patch' },
message: 'should pass "$patch" keyword validation'
}
]
subSchema: {
properties: { resourceType: { const: 'Patient' } },
additionalProperties: false,
required: [ 'resourceType' ]
}
subSchema isValid: true
subSchema errors: null
```
```sh
test: { resourceType: 'Patient', q: 'abc' }
rootSchema isValid: false
rootSchema errors: [
{
keyword: 'required',
dataPath: '',
schemaPath: '#/required',
params: { missingProperty: 'q' },
message: "should have required property 'q'" # <-- IT HAS PROPERTY q!
},
{
keyword: '$patch',
dataPath: '',
schemaPath: '#/$patch',
params: { keyword: '$patch' },
message: 'should pass "$patch" keyword validation'
}
]
subSchema: {
properties: { resourceType: { const: 'Patient' } },
additionalProperties: false,
required: [ 'resourceType' ]
}
subSchema isValid: true # <-- WHY? q HAS THE WRONG TYPE
subSchema errors: null
```
```sh
test: { resourceType: 'Patient' }
rootSchema isValid: false
rootSchema errors: [
{
keyword: 'required',
dataPath: '',
schemaPath: '#/required',
params: { missingProperty: 'q' },
message: "should have required property 'q'"
},
{
keyword: '$patch',
dataPath: '',
schemaPath: '#/$patch',
params: { keyword: '$patch' },
message: 'should pass "$patch" keyword validation'
}
]
subSchema: {
properties: { resourceType: { const: 'Patient' } },
additionalProperties: false,
required: [ 'resourceType' ]
}
subSchema isValid: true # <-- WHY? q IS MISSING!
subSchema errors: null
```

As you can see, testing against the rootSchema doens't differentiate between the various definitions, but testing against the subSchemas doesn't apply the patch.

I have tried various combinations of `ajv.compile` and `ajv.schema`, but nothing seems to give the results I'm expecting.

贡献指南

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

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

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