Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: disallow condition depending on the containing property #128

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@
"allOf",
0,
"items",
"properties",
"condition",
"allOf",
1,
"definitions",
"condition"
],
Expand All @@ -149,5 +149,35 @@
"property": "missing property name for condition"
}
}
},
{
"path": [
"definitions",
"properties",
"allOf",
0,
"items",
"allOf",
1,
"allOf",
0,
"then"
],
"errorMessage": "Invalid condition.property, must be different than property.id"
},
{
"path": [
"definitions",
"properties",
"allOf",
0,
"items",
"allOf",
1,
"allOf",
1,
"then"
],
"errorMessage": "Invalid condition.property, must be different than property.id"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
"choices"
]
}
},
{
"$ref": "condition.json"
}
],
"properties": {
"id": {
"type": "string",
"description": "Unique identifier of the property."
},
"value": {
"$id": "#/properties/property/value",
"type": [
Expand Down Expand Up @@ -80,7 +79,7 @@
"description": "The value of a choice."
},
"condition": {
"$ref": "condition.json"
"$ref": "condition.json#/properties/condition"
}
},
"required": [
Expand Down Expand Up @@ -143,9 +142,6 @@
"$id": "#/properties/property/group",
"type": "string",
"description": "The custom group of a control field."
},
"condition": {
"$ref": "condition.json"
}
}
}
Expand Down
129 changes: 107 additions & 22 deletions packages/element-templates-json-schema-shared/src/defs/condition.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
{
"$id": "#/condition",
"type": "object",
"description": "Condition(s) to activate the binding.",
"allOf": [
{
"$ref": "examples.json#/condition"
}
],
"definitions": {
"condition": {
"type": "object",
Expand Down Expand Up @@ -69,26 +61,119 @@
]
}
]
},
"conditionDependingOnId": {
"anyOf": [
{
"required": [
"property"
],
"properties": {
"property": {
"const": {
"$data": "2/id"
}
}
}
},
{
"required": [
"allMatch"
],
"allMatch": {
"contains": {
"properties": {
"property": {
"const": {
"$data": "2/id"
}
}
}
}
}
}
]
}
},
"oneOf": [
"allOf": [
{
"$ref": "#/definitions/condition"
"$comment": "property#condition should not depend on property#id",
"if": {
"required": [
"id",
"condition"
],
"properties": {
"condition": {
"$ref": "#/definitions/conditionDependingOnId"
}
}
},
"then": {
"not": {
"required": [ "condition" ]
}
}
},
{
"properties": {
"allMatch": {
"$id": "#/allMatch",
"type": "array",
"items": {
"$ref": "#/definitions/condition"
},
"minItems": 1
"$comment": "property#condition should not depend on property#id",
"if": {
"required": [
"id",
"choices"
],
"properties": {
"choices": {
"contains": {
"properties": {
"condition": {
"$ref": "#/definitions/conditionDependingOnId"
}
}
}
}
}
},
"required": [
"allMatch"
"then": {
"not": {
"required": [ "choices" ]
}
}
}
],
"properties": {
"id": {
"type": "string",
"description": "Unique identifier of the property."
},
"condition": {
"type": "object",
"description": "Condition(s) to activate the binding.",
"allOf": [
{
"$ref": "examples.json#/condition"
}
],
"oneOf": [
{
"$ref": "#/definitions/condition"
},
{
"properties": {
"allMatch": {
"$id": "#/allMatch",
"type": "array",
"items": {
"$ref": "#/definitions/condition"
},
"minItems": 1
}
},
"required": [
"allMatch"
]
}
]
}
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ function createValidator(schema, errors) {

const ajv = new Ajv({
allErrors: true,
strict: false
strict: false,
$data: true
});

AjvErrors(ajv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,42 @@ export const errors = [
{
keyword: 'required',
dataPath: '/properties/1/condition',
schemaPath: '#/definitions/properties/allOf/0/items/properties/condition/definitions/condition/oneOf/0/required',
schemaPath: '#/definitions/properties/allOf/0/items/allOf/1/definitions/condition/oneOf/0/required',
params: { missingProperty: 'equals' },
message: "should have required property 'equals'"
},
{
keyword: 'required',
dataPath: '/properties/1/condition',
schemaPath: '#/definitions/properties/allOf/0/items/properties/condition/definitions/condition/oneOf/1/required',
schemaPath: '#/definitions/properties/allOf/0/items/allOf/1/definitions/condition/oneOf/1/required',
params: { missingProperty: 'oneOf' },
message: "should have required property 'oneOf'"
},
{
dataPath: '/properties/1/condition',
keyword: 'required',
message: "should have required property 'isActive'",
params: {
missingProperty: 'isActive'
},
schemaPath: '#/definitions/properties/allOf/0/items/properties/condition/definitions/condition/oneOf/2/required'
dataPath: '/properties/1/condition',
schemaPath: '#/definitions/properties/allOf/0/items/allOf/1/definitions/condition/oneOf/2/required',
params: { missingProperty: 'isActive' },
message: "should have required property 'isActive'"
},
{
keyword: 'oneOf',
dataPath: '/properties/1/condition',
schemaPath: '#/definitions/properties/allOf/0/items/properties/condition/definitions/condition/oneOf',
schemaPath: '#/definitions/properties/allOf/0/items/allOf/1/definitions/condition/oneOf',
params: { passingSchemas: null },
message: 'should match exactly one schema in oneOf'
},
{
keyword: 'required',
dataPath: '/properties/1/condition',
schemaPath: '#/allOf/0/items/properties/condition/oneOf/1/required',
schemaPath: '#/allOf/0/items/allOf/1/properties/condition/oneOf/1/required',
params: { missingProperty: 'allMatch' },
message: "should have required property 'allMatch'"
},
{
keyword: 'oneOf',
dataPath: '/properties/1/condition',
schemaPath: '#/allOf/0/items/properties/condition/oneOf',
schemaPath: '#/allOf/0/items/allOf/1/properties/condition/oneOf',
params: { passingSchemas: null },
message: 'should match exactly one schema in oneOf'
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,44 +33,42 @@ export const errors = [
{
keyword: 'required',
dataPath: '/properties/1/condition',
schemaPath: '#/definitions/properties/allOf/0/items/properties/condition/definitions/condition/oneOf/0/required',
schemaPath: '#/definitions/properties/allOf/0/items/allOf/1/definitions/condition/oneOf/0/required',
params: { missingProperty: 'equals' },
message: "should have required property 'equals'"
},
{
keyword: 'required',
dataPath: '/properties/1/condition',
schemaPath: '#/definitions/properties/allOf/0/items/properties/condition/definitions/condition/oneOf/1/required',
schemaPath: '#/definitions/properties/allOf/0/items/allOf/1/definitions/condition/oneOf/1/required',
params: { missingProperty: 'oneOf' },
message: "should have required property 'oneOf'"
},
{
dataPath: '/properties/1/condition',
keyword: 'required',
message: "should have required property 'isActive'",
params: {
missingProperty: 'isActive'
},
schemaPath: '#/definitions/properties/allOf/0/items/properties/condition/definitions/condition/oneOf/2/required'
dataPath: '/properties/1/condition',
schemaPath: '#/definitions/properties/allOf/0/items/allOf/1/definitions/condition/oneOf/2/required',
params: { missingProperty: 'isActive' },
message: "should have required property 'isActive'"
},
{
keyword: 'oneOf',
dataPath: '/properties/1/condition',
schemaPath: '#/definitions/properties/allOf/0/items/properties/condition/definitions/condition/oneOf',
schemaPath: '#/definitions/properties/allOf/0/items/allOf/1/definitions/condition/oneOf',
params: { passingSchemas: null },
message: 'should match exactly one schema in oneOf'
},
{
keyword: 'required',
dataPath: '/properties/1/condition',
schemaPath: '#/allOf/0/items/properties/condition/oneOf/1/required',
schemaPath: '#/allOf/0/items/allOf/1/properties/condition/oneOf/1/required',
params: { missingProperty: 'allMatch' },
message: "should have required property 'allMatch'"
},
{
keyword: 'oneOf',
dataPath: '/properties/1/condition',
schemaPath: '#/allOf/0/items/properties/condition/oneOf',
schemaPath: '#/allOf/0/items/allOf/1/properties/condition/oneOf',
params: { passingSchemas: null },
message: 'should match exactly one schema in oneOf'
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ export const errors = [
{
keyword: 'errorMessage',
dataPath: '/properties/1/condition',
schemaPath: '#/definitions/properties/allOf/0/items/properties/condition/definitions/condition/errorMessage',
schemaPath: '#/definitions/properties/allOf/0/items/allOf/1/definitions/condition/errorMessage',
params: {
errors: [
{
keyword: 'required',
dataPath: '/properties/1/condition',
schemaPath: '#/definitions/properties/allOf/0/items/properties/condition/definitions/condition/required',
schemaPath: '#/definitions/properties/allOf/0/items/allOf/1/definitions/condition/required',
params: { missingProperty: 'property' },
message: "should have required property 'property'",
emUsed: true
Expand All @@ -51,14 +51,14 @@ export const errors = [
{
keyword: 'required',
dataPath: '/properties/1/condition',
schemaPath: '#/allOf/0/items/properties/condition/oneOf/1/required',
schemaPath: '#/allOf/0/items/allOf/1/properties/condition/oneOf/1/required',
params: { missingProperty: 'allMatch' },
message: "should have required property 'allMatch'"
},
{
keyword: 'oneOf',
dataPath: '/properties/1/condition',
schemaPath: '#/allOf/0/items/properties/condition/oneOf',
schemaPath: '#/allOf/0/items/allOf/1/properties/condition/oneOf',
params: { passingSchemas: null },
message: 'should match exactly one schema in oneOf'
},
Expand Down
Loading