Skip to content

Commit

Permalink
fix: Verify collection processing properties everywhere
Browse files Browse the repository at this point in the history
This makes sure that *any* invalid processing property causes a validation
error.
  • Loading branch information
l0b0 committed Jul 25, 2023
1 parent ffe65dd commit 11a8037
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 43 deletions.
88 changes: 45 additions & 43 deletions json-schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@
"const": "Feature"
},
"properties": {
"allOf": [
{
"$ref": "#/definitions/require_any_field"
},
{
"$ref": "#/definitions/fields"
}
]
"$ref": "#/definitions/fields"
},
"assets": {
"$comment": "This validates the fields in Item Assets, but does not require them.",
Expand Down Expand Up @@ -75,9 +68,7 @@
"allOf": [
{
"$ref": "#/definitions/stac_extensions"
}
],
"anyOf": [
},
{
"$comment": "Requires at least one provider to contain processing fields.",
"type": "object",
Expand Down Expand Up @@ -108,7 +99,7 @@
}
},
{
"$ref": "#/definitions/require_any_field"
"$ref": "#/definitions/fields"
}
]
}
Expand All @@ -118,50 +109,70 @@
{
"$comment": "Requires at least one asset to contain processing fields.",
"type": "object",
"required": [
"assets"
],
"properties": {
"assets": {
"type": "object",
"not": {
"additionalProperties": {
"not": {
"$ref": "#/definitions/require_any_field"
}
}
"additionalProperties": {
"$ref": "#/definitions/fields"
}
}
}
},
{
"$comment": "Requires at least one item asset definition to contain processing fields.",
"type": "object",
"required": [
"item_assets"
],
"properties": {
"item_assets": {
"type": "object",
"not": {
"additionalProperties": {
"not": {
"$ref": "#/definitions/require_any_field"
}
}
"additionalProperties": {
"$ref": "#/definitions/fields"
}
}
}
},
{
"type": "object",
"$comment": "Requires at least one summary to be a processing field.",
"required": [
"summaries"
],
"properties": {
"summaries": {
"$ref": "#/definitions/require_any_field"
"type": "object",
"properties": {
"processing:expression": {
"type": "array",
"items": {
"$ref": "#/definitions/fields/properties/processing:expression"
},
"minItems": 1
},
"processing:facility": {
"type": "array",
"items": {
"$ref": "#/definitions/fields/properties/processing:facility"
},
"minItems": 1
},
"processing:level": {
"type": "array",
"items": {
"$ref": "#/definitions/fields/properties/processing:level"
},
"minItems": 1
},
"processing:lineage": {
"type": "array",
"items": {
"$ref": "#/definitions/fields/properties/processing:lineage"
},
"minItems": 1
},
"processing:software": {
"type": "array",
"items": {
"$ref": "#/definitions/fields/properties/processing:software"
},
"minItems": 1
}
}
}
}
}
Expand Down Expand Up @@ -200,15 +211,6 @@
}
}
},
"require_any_field": {
"anyOf": [
{"type": "object", "required": ["processing:expression"]},
{"type": "object", "required": ["processing:lineage"]},
{"type": "object", "required": ["processing:level"]},
{"type": "object", "required": ["processing:facility"]},
{"type": "object", "required": ["processing:software"]}
]
},
"fields": {
"type": "object",
"properties": {
Expand Down
154 changes: 154 additions & 0 deletions tests/collection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,158 @@ describe('Collection example', () => {

expect(valid).toBeTruthy();
});

it('should fail validation when providers processing expression is invalid', async () => {
// given
example.providers[0]['processing:expression'] = null;

// when
let valid = validate(example);

// then
expect(valid).toBeFalsy();
expect(
validate.errors.some(
(error) =>
error.instancePath === '/providers/0/processing:expression'
&& error.message === 'must be object',
)
).toBeTruthy();
});

it('should fail validation when asset processing expression is invalid', async () => {
// given
example.assets = {
'example': {
'href': 'https://example.org/file.xyz',
'processing:expression': null,
}
};

// when
let valid = validate(example);

// then
expect(valid).toBeFalsy();
expect(
validate.errors.some(
(error) =>
error.instancePath === '/assets/example/processing:expression'
&& error.message === 'must be object',
)
).toBeTruthy();
});

it('should fail validation when item asset processing expression is invalid', async () => {
// given
example.item_assets = {
'example': {
'href': 'https://example.org/file.xyz',
'processing:expression': null,
}
};

// when
let valid = validate(example);

// then
expect(valid).toBeFalsy();
expect(
validate.errors.some(
(error) =>
error.instancePath === '/item_assets/example/processing:expression'
&& error.message === 'must be object',
)
).toBeTruthy();
});

it('should fail validation when summary processing expression is invalid', async () => {
// given
example.summaries['processing:expression'] = null;

// when
let valid = validate(example);

// then
expect(valid).toBeFalsy();
expect(
validate.errors.some(
(error) =>
error.instancePath === '/summaries/processing:expression'
&& error.message === 'must be array',
)
).toBeTruthy();
});

it('should fail validation when summary processing facility is invalid', async () => {
// given
example.summaries['processing:facility'] = null;

// when
let valid = validate(example);

// then
expect(valid).toBeFalsy();
expect(
validate.errors.some(
(error) =>
error.instancePath === '/summaries/processing:facility'
&& error.message === 'must be array',
)
).toBeTruthy();
});

it('should fail validation when summary processing level is invalid', async () => {
// given
example.summaries['processing:level'] = null;

// when
let valid = validate(example);

// then
expect(valid).toBeFalsy();
expect(
validate.errors.some(
(error) =>
error.instancePath === '/summaries/processing:level'
&& error.message === 'must be array',
)
).toBeTruthy();
});

it('should fail validation when summary processing lineage is invalid', async () => {
// given
example.summaries['processing:lineage'] = null;

// when
let valid = validate(example);

// then
expect(valid).toBeFalsy();
expect(
validate.errors.some(
(error) =>
error.instancePath === '/summaries/processing:lineage'
&& error.message === 'must be array',
)
).toBeTruthy();
});

it('should fail validation when summary processing software is invalid', async () => {
// given
example.summaries['processing:software'] = null;

// when
let valid = validate(example);

// then
expect(valid).toBeFalsy();
expect(
validate.errors.some(
(error) =>
error.instancePath === '/summaries/processing:software'
&& error.message === 'must be array',
)
).toBeTruthy();
});
});

0 comments on commit 11a8037

Please sign in to comment.