From f8a6202ddf38e94b9abff607b3731659b7e860ac Mon Sep 17 00:00:00 2001 From: Matthias Mohr Date: Thu, 13 Aug 2020 11:15:56 +0200 Subject: [PATCH] Code refactoring, fixed https://github.com/Open-EO/openeo-web-editor/issues/107 --- README.md | 2 +- assets/subtype-schemas.json | 838 ++++++++++++++++++------------------ package.json | 2 +- src/jsonschema.js | 55 +-- src/node.js | 8 +- src/process.js | 2 +- src/processgraph.js | 69 +-- tests/jsonschema.test.js | 5 - 8 files changed, 445 insertions(+), 536 deletions(-) diff --git a/README.md b/README.md index caba76f..d37ce76 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This library's version is **1.0.0-beta.3** and supports **openEO API version 1.0.x**. -This repository was split up from [openeo-js-commons](https://github.com/Open-EO/openeo-js-commons). Old releases can be found there. +This repository was split apart from [openeo-js-commons](https://github.com/Open-EO/openeo-js-commons). Old releases can be found there. ## Features - Parsing a process graph diff --git a/assets/subtype-schemas.json b/assets/subtype-schemas.json index 378d655..475bba8 100644 --- a/assets/subtype-schemas.json +++ b/assets/subtype-schemas.json @@ -1,421 +1,421 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "$id": "http://processes.openeo.org/1.0.0/meta/subtype-schemas.json", - "title": "Subtype Schemas", - "description": "This file defines the schemas for subtypes we define for openEO processes.", - "definitions": { - "band-name": { - "type": "string", - "subtype": "band-name", - "title": "Band Name", - "description": "Either a unique band name (metadata field `name`) or a [common band name](https://github.com/radiantearth/stac-spec/tree/v0.9.0/extensions/eo#common-band-names) (metadata field `common_name`) available in the data cube. If unique band name and common name conflict, the unique band name has higher priority." - }, - "bounding-box": { - "type": "object", - "subtype": "bounding-box", - "title": "Bounding Box", - "description": "A bounding box with the required fields `west`, `south`, `east`, `north` and optionally `base`, `height`, `crs`. The `crs` is a EPSG code, a WKT2:2018 string or a PROJ definition (deprecated).", - "required": [ - "west", - "south", - "east", - "north" - ], - "properties": { - "west": { - "description": "West (lower left corner, coordinate axis 1).", - "type": "number" - }, - "south": { - "description": "South (lower left corner, coordinate axis 2).", - "type": "number" - }, - "east": { - "description": "East (upper right corner, coordinate axis 1).", - "type": "number" - }, - "north": { - "description": "North (upper right corner, coordinate axis 2).", - "type": "number" - }, - "base": { - "description": "Base (optional, lower left corner, coordinate axis 3).", - "type": [ - "number", - "null" - ], - "default": null - }, - "height": { - "description": "Height (optional, upper right corner, coordinate axis 3).", - "type": [ - "number", - "null" - ], - "default": null - }, - "crs": { - "description": "Coordinate reference system of the extent, specified as as [EPSG code](http://www.epsg-registry.org/), [WKT2 (ISO 19162) string](http://docs.opengeospatial.org/is/18-010r7/18-010r7.html) or [PROJ definition (deprecated)](https://proj.org/usage/quickstart.html). Defaults to `4326` (EPSG code 4326) unless the client explicitly requests a different coordinate reference system.", - "anyOf": [ - { - "$ref": "#/definitions/epsg-code" - }, - { - "$ref": "#/definitions/wkt2-definition" - }, - { - "$ref": "#/definitions/proj-definition" - } - ], - "default": 4326 - } - } - }, - "chunk-size": { - "type": "object", - "subtype": "chunk-size", - "required": [ - "dimension", - "value" - ], - "properties": { - "dimension": { - "type": "string" - }, - "value": { - "default": null, - "oneOf": [ - { - "type": "null" - }, - { - "type": "number", - "minimum": 0 - }, - { - "$ref": "#/definitions/duration" - } - ] - }, - "unit": { - "type": "string", - "description": "The unit the values are given in. If no unit is given, uses the unit specified for the dimension or otherwise the default unit of the reference system." - } - } - }, - "collection-id": { - "type": "string", - "subtype": "collection-id", - "title": "Collection ID", - "description": "A collection id from the list of supported collections.", - "pattern": "^[\\w\\-\\.~/]+$" - }, - "date": { - "type": "string", - "subtype": "date", - "format": "date", - "title": "Date only", - "description": "Date only representation, as defined for `full-date` by [RFC 3339 in section 5.6](https://tools.ietf.org/html/rfc3339#section-5.6). The time zone is UTC." - }, - "date-time": { - "type": "string", - "subtype": "date-time", - "format": "date-time", - "title": "Date with Time", - "description": "Date and time representation, as defined for `date-time` by [RFC 3339 in section 5.6](https://tools.ietf.org/html/rfc3339#section-5.6)." - }, - "duration": { - "type": "string", - "subtype": "duration", - "description": "[ISO 8601 duration](https://en.wikipedia.org/wiki/ISO_8601#Durations), e.g. `P1D` for one day.", - "pattern": "^(-?)P(?=\\d|T\\d)(?:(\\d+)Y)?(?:(\\d+)M)?(?:(\\d+)([DW]))?(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:(\\d+(?:\\.\\d+)?)S)?)?$" - }, - "epsg-code": { - "type": "integer", - "subtype": "epsg-code", - "title": "EPSG Code", - "description": "Specifies details about cartographic projections as [EPSG](http://www.epsg.org) code.", - "minimum": 1000, - "examples": [ - 3857 - ] - }, - "file-path": { - "type": "string", - "subtype": "file-path", - "title": "Single File path", - "description": "A relative path to a user-uploaded file. Folders can't be specified." - }, - "file-paths": { - "type": "array", - "subtype": "file-paths", - "title": "Multiple File paths", - "description": "An array with relative paths to user-uploaded files. Clients should assist to generate a list of files for folders.", - "items": { - "$ref": "#/definitions/file-path" - } - }, - "geojson": { - "type": "object", - "subtype": "geojson", - "title": "GeoJSON", - "description": "GeoJSON as defined by [RFC 7946](https://tools.ietf.org/html/rfc7946).", - "allOf": [ - { - "$ref": "https://geojson.org/schema/GeoJSON.json" - } - ] - }, - "input-format": { - "type": "string", - "subtype": "input-format", - "title": "Input File Format", - "description": "An input format supported by the back-end." - }, - "input-format-options": { - "type": "object", - "subtype": "input-format-options", - "title": "Options for Input File Formats", - "description": "Key-value-pairs with arguments for the input format options supported by the back-end." - }, - "job-id": { - "type": "string", - "subtype": "job-id", - "title": "Batch Job ID", - "description": "A batch job id, either one of the jobs a user has stored or a publicly available job.", - "pattern": "^[\\w\\-\\.~]+$" - }, - "kernel": { - "type": "array", - "subtype": "kernel", - "title": "Image Kernel", - "description": "Image kernel, a two-dimensional array of numbers.", - "items": { - "type": "array", - "items": { - "type": "number" - } - } - }, - "labeled-array": { - "type": "array", - "subtype": "labeled-array", - "title": "Array with labels", - "description": "An associative, ordered list of key-value pairs. Basically an array, which additionally has labels for each value. Labels can be numbers or (temporal) strings.\n\n**Important:** Due to the lack of such a data type in JSON, the schema for the items is not formally specified yet and a placeholder.", - "items": { - "description": "Any data type." - } - }, - "metadata-filter": { - "type": "object", - "subtype": "metadata-filter", - "title": "Filters", - "description": "A list of filters to check against. Specify key-value-pairs with the key being the name of the metadata property name and the value being a user-defined process evaluated against the metadata values.", - "additionalProperties": { - "type": "object", - "subtype": "process-graph", - "parameters": [ - { - "name": "value", - "description": "The property value to be checked against.", - "schema": { - "description": "Any data type." - } - } - ] - } - }, - "output-format": { - "type": "string", - "subtype": "output-format", - "title": "Output File Format", - "description": "An output format supported by the back-end." - }, - "output-format-options": { - "type": "object", - "subtype": "output-format-options", - "title": "Options for Output File Formats", - "description": "Key-value-pairs with arguments for the output format options supported by the back-end." - }, - "process-graph": { - "type": "object", - "subtype": "process-graph", - "title": "User-defined process", - "description": "An process graph that is passed as an argument and is expected to be executed by the process. Parameters passed to the process graph are specified in the `parameters` property of the corresponding schema.", - "required": [ - "process_graph" - ], - "properties": { - "process_graph": { - "type": "object", - "additionalProperties": { - "type": "object", - "required": [ - "process_id", - "arguments" - ], - "properties": { - "process_id": { - "type": "string" - }, - "arguments": {} - } - } - } - } - }, - "proj-definition": { - "type": "string", - "subtype": "proj-definition", - "title": "PROJ definition", - "description": "**DEPRECATED.** Specifies details about cartographic projections as [PROJ](https://proj.org/usage/quickstart.html) definition." - }, - "raster-cube": { - "type": "object", - "subtype": "raster-cube", - "title": "Raster data cube", - "description": "A raster data cube, an image collection stored at the back-end. Different back-ends have different internal representations for this data structure." - }, - "temporal-interval": { - "type": "array", - "subtype": "temporal-interval", - "title": "Single temporal interval", - "description": "Left-closed temporal interval, represented as two-element array with the following elements:\n\n1. The first element is the start of the temporal interval. The specified instance in time is **included** in the interval.\n2. The second element is the end of the temporal interval. The specified instance in time is **excluded** from the interval.\n\nThe specified temporal strings follow [RFC 3339](https://tools.ietf.org/html/rfc3339). Although [RFC 3339 prohibits the hour to be '24'](https://tools.ietf.org/html/rfc3339#section-5.7), **this process allows the value '24' for the hour** of an end time in order to make it possible that left-closed time intervals can fully cover the day. `null` can be used to specify open intervals.", - "minItems": 2, - "maxItems": 2, - "items": { - "description": "Processes and implementations may choose to only implement a subset of the subtypes specified here. Clients must check what back-ends / processes actually support.", - "anyOf": [ - { - "$ref": "#/definitions/date-time" - }, - { - "$ref": "#/definitions/date" - }, - { - "$ref": "#/definitions/time" - }, - { - "$ref": "#/definitions/year" - }, - { - "type": "null" - } - ] - }, - "examples": [ - [ - "2015-01-01T00:00:00Z", - "2016-01-01T00:00:00Z" - ], - [ - "2015-01-01", - "2016-01-01" - ], - [ - "00:00:00Z", - "12:00:00Z" - ], - [ - "2015-01-01", - null - ] - ] - }, - "temporal-intervals": { - "type": "array", - "subtype": "temporal-intervals", - "title": "Multiple temporal intervals", - "description": "Left-closed temporal intervals, which are allowed to overlap. Formatted as array of two-element arrays, each being an array with subtype `temporal-interval`.", - "items": { - "$ref": "#/definitions/temporal-interval" - }, - "minItems": 1, - "examples": [ - [ - [ - "2015-01-01", - "2016-01-01" - ], - [ - "2016-01-01", - "2017-01-01" - ], - [ - "2017-01-01", - "2018-01-01" - ] - ], - [ - [ - "00:00:00Z", - "12:00:00Z" - ], - [ - "12:00:00Z", - "24:00:00Z" - ] - ], - [ - [ - "2020-01-01T00:00:00Z", - null - ] - ] - ] - }, - "time": { - "type": "string", - "subtype": "time", - "format": "time", - "title": "Time only", - "description": "Time only representation, as defined for `full-time` by [RFC 3339 in section 5.6](https://tools.ietf.org/html/rfc3339#section-5.6). Although [RFC 3339 prohibits the hour to be '24'](https://tools.ietf.org/html/rfc3339#section-5.7), this definition allows the value '24' for the hour as end time in an interval in order to make it possible that left-closed time intervals can fully cover the day." - }, - "udf-code": { - "type": "string", - "subtype": "udf-code", - "title": "UDF source code", - "description": "The (multi-line) source code of an user-defined function (UDF)." - }, - "udf-runtime": { - "type": "string", - "subtype": "udf-runtime", - "title": "UDF runtime", - "description": "The name of an UDF runtime." - }, - "udf-runtime-version": { - "type": "string", - "subtype": "udf-runtime-version", - "title": "UDF Runtime version", - "description": "The version of an UDF runtime." - }, - "uri": { - "type": "string", - "subtype": "uri", - "format": "uri", - "title": "URI", - "description": "A valid URI according to [RFC3986](https://tools.ietf.org/html/rfc3986)." - }, - "vector-cube": { - "type": "object", - "subtype": "vector-cube", - "title": "Vector data cube", - "description": "A vector data cube, a vector collection stored at the back-end. Different back-ends have different internal representations for this data structure" - }, - "wkt2-definition": { - "type": "string", - "subtype": "wkt2-definition", - "title": "WKT2 definition", - "description": "Specifies details about cartographic projections as WKT2 string. Refers to the latest WKT2 version (currently [WKT2:2018](http://docs.opengeospatial.org/is/18-010r7/18-010r7.html) / ISO 19162:2018) unless otherwise stated by the process." - }, - "year": { - "type": "string", - "subtype": "year", - "minLength": 4, - "maxLength": 4, - "pattern": "^\\d{4}$", - "title": "Year only", - "description": "Year representation, as defined for `date-fullyear` by [RFC 3339 in section 5.6](https://tools.ietf.org/html/rfc3339#section-5.6)." - } - } + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "http://processes.openeo.org/1.0.0/meta/subtype-schemas.json", + "title": "Subtype Schemas", + "description": "This file defines the schemas for subtypes we define for openEO processes.", + "definitions": { + "band-name": { + "type": "string", + "subtype": "band-name", + "title": "Band Name", + "description": "Either a unique band name (metadata field `name`) or a [common band name](https://github.com/radiantearth/stac-spec/tree/v0.9.0/extensions/eo#common-band-names) (metadata field `common_name`) available in the data cube. If unique band name and common name conflict, the unique band name has higher priority." + }, + "bounding-box": { + "type": "object", + "subtype": "bounding-box", + "title": "Bounding Box", + "description": "A bounding box with the required fields `west`, `south`, `east`, `north` and optionally `base`, `height`, `crs`. The `crs` is a EPSG code, a WKT2:2018 string or a PROJ definition (deprecated).", + "required": [ + "west", + "south", + "east", + "north" + ], + "properties": { + "west": { + "description": "West (lower left corner, coordinate axis 1).", + "type": "number" + }, + "south": { + "description": "South (lower left corner, coordinate axis 2).", + "type": "number" + }, + "east": { + "description": "East (upper right corner, coordinate axis 1).", + "type": "number" + }, + "north": { + "description": "North (upper right corner, coordinate axis 2).", + "type": "number" + }, + "base": { + "description": "Base (optional, lower left corner, coordinate axis 3).", + "type": [ + "number", + "null" + ], + "default": null + }, + "height": { + "description": "Height (optional, upper right corner, coordinate axis 3).", + "type": [ + "number", + "null" + ], + "default": null + }, + "crs": { + "description": "Coordinate reference system of the extent, specified as as [EPSG code](http://www.epsg-registry.org/), [WKT2 (ISO 19162) string](http://docs.opengeospatial.org/is/18-010r7/18-010r7.html) or [PROJ definition (deprecated)](https://proj.org/usage/quickstart.html). Defaults to `4326` (EPSG code 4326) unless the client explicitly requests a different coordinate reference system.", + "anyOf": [ + { + "$ref": "#/definitions/epsg-code" + }, + { + "$ref": "#/definitions/wkt2-definition" + }, + { + "$ref": "#/definitions/proj-definition" + } + ], + "default": 4326 + } + } + }, + "chunk-size": { + "type": "object", + "subtype": "chunk-size", + "required": [ + "dimension", + "value" + ], + "properties": { + "dimension": { + "type": "string" + }, + "value": { + "default": null, + "oneOf": [ + { + "type": "null" + }, + { + "type": "number", + "minimum": 0 + }, + { + "$ref": "#/definitions/duration" + } + ] + }, + "unit": { + "type": "string", + "description": "The unit the values are given in. If no unit is given, uses the unit specified for the dimension or otherwise the default unit of the reference system." + } + } + }, + "collection-id": { + "type": "string", + "subtype": "collection-id", + "title": "Collection ID", + "description": "A collection id from the list of supported collections.", + "pattern": "^[\\w\\-\\.~/]+$" + }, + "date": { + "type": "string", + "subtype": "date", + "format": "date", + "title": "Date only", + "description": "Date only representation, as defined for `full-date` by [RFC 3339 in section 5.6](https://tools.ietf.org/html/rfc3339#section-5.6). The time zone is UTC." + }, + "date-time": { + "type": "string", + "subtype": "date-time", + "format": "date-time", + "title": "Date with Time", + "description": "Date and time representation, as defined for `date-time` by [RFC 3339 in section 5.6](https://tools.ietf.org/html/rfc3339#section-5.6)." + }, + "duration": { + "type": "string", + "subtype": "duration", + "description": "[ISO 8601 duration](https://en.wikipedia.org/wiki/ISO_8601#Durations), e.g. `P1D` for one day.", + "pattern": "^(-?)P(?=\\d|T\\d)(?:(\\d+)Y)?(?:(\\d+)M)?(?:(\\d+)([DW]))?(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:(\\d+(?:\\.\\d+)?)S)?)?$" + }, + "epsg-code": { + "type": "integer", + "subtype": "epsg-code", + "title": "EPSG Code", + "description": "Specifies details about cartographic projections as [EPSG](http://www.epsg.org) code.", + "minimum": 1000, + "examples": [ + 3857 + ] + }, + "file-path": { + "type": "string", + "subtype": "file-path", + "title": "Single File path", + "description": "A relative path to a user-uploaded file. Folders can't be specified." + }, + "file-paths": { + "type": "array", + "subtype": "file-paths", + "title": "Multiple File paths", + "description": "An array with relative paths to user-uploaded files. Clients should assist to generate a list of files for folders.", + "items": { + "$ref": "#/definitions/file-path" + } + }, + "geojson": { + "type": "object", + "subtype": "geojson", + "title": "GeoJSON", + "description": "GeoJSON as defined by [RFC 7946](https://tools.ietf.org/html/rfc7946).", + "allOf": [ + { + "$ref": "https://geojson.org/schema/GeoJSON.json" + } + ] + }, + "input-format": { + "type": "string", + "subtype": "input-format", + "title": "Input File Format", + "description": "An input format supported by the back-end." + }, + "input-format-options": { + "type": "object", + "subtype": "input-format-options", + "title": "Options for Input File Formats", + "description": "Key-value-pairs with arguments for the input format options supported by the back-end." + }, + "job-id": { + "type": "string", + "subtype": "job-id", + "title": "Batch Job ID", + "description": "A batch job id, either one of the jobs a user has stored or a publicly available job.", + "pattern": "^[\\w\\-\\.~]+$" + }, + "kernel": { + "type": "array", + "subtype": "kernel", + "title": "Image Kernel", + "description": "Image kernel, a two-dimensional array of numbers.", + "items": { + "type": "array", + "items": { + "type": "number" + } + } + }, + "labeled-array": { + "type": "array", + "subtype": "labeled-array", + "title": "Array with labels", + "description": "An associative, ordered list of key-value pairs. Basically an array, which additionally has labels for each value. Labels can be numbers or (temporal) strings.\n\n**Important:** Due to the lack of such a data type in JSON, the schema for the items is not formally specified yet and a placeholder.", + "items": { + "description": "Any data type." + } + }, + "metadata-filter": { + "type": "object", + "subtype": "metadata-filter", + "title": "Filters", + "description": "A list of filters to check against. Specify key-value-pairs with the key being the name of the metadata property name and the value being a user-defined process evaluated against the metadata values.", + "additionalProperties": { + "type": "object", + "subtype": "process-graph", + "parameters": [ + { + "name": "value", + "description": "The property value to be checked against.", + "schema": { + "description": "Any data type." + } + } + ] + } + }, + "output-format": { + "type": "string", + "subtype": "output-format", + "title": "Output File Format", + "description": "An output format supported by the back-end." + }, + "output-format-options": { + "type": "object", + "subtype": "output-format-options", + "title": "Options for Output File Formats", + "description": "Key-value-pairs with arguments for the output format options supported by the back-end." + }, + "process-graph": { + "type": "object", + "subtype": "process-graph", + "title": "User-defined process", + "description": "An process graph that is passed as an argument and is expected to be executed by the process. Parameters passed to the process graph are specified in the `parameters` property of the corresponding schema.", + "required": [ + "process_graph" + ], + "properties": { + "process_graph": { + "type": "object", + "additionalProperties": { + "type": "object", + "required": [ + "process_id", + "arguments" + ], + "properties": { + "process_id": { + "type": "string" + }, + "arguments": {} + } + } + } + } + }, + "proj-definition": { + "type": "string", + "subtype": "proj-definition", + "title": "PROJ definition", + "description": "**DEPRECATED.** Specifies details about cartographic projections as [PROJ](https://proj.org/usage/quickstart.html) definition." + }, + "raster-cube": { + "type": "object", + "subtype": "raster-cube", + "title": "Raster data cube", + "description": "A raster data cube, an image collection stored at the back-end. Different back-ends have different internal representations for this data structure." + }, + "temporal-interval": { + "type": "array", + "subtype": "temporal-interval", + "title": "Single temporal interval", + "description": "Left-closed temporal interval, represented as two-element array with the following elements:\n\n1. The first element is the start of the temporal interval. The specified instance in time is **included** in the interval.\n2. The second element is the end of the temporal interval. The specified instance in time is **excluded** from the interval.\n\nThe specified temporal strings follow [RFC 3339](https://tools.ietf.org/html/rfc3339). Although [RFC 3339 prohibits the hour to be '24'](https://tools.ietf.org/html/rfc3339#section-5.7), **this process allows the value '24' for the hour** of an end time in order to make it possible that left-closed time intervals can fully cover the day. `null` can be used to specify open intervals.", + "minItems": 2, + "maxItems": 2, + "items": { + "description": "Processes and implementations may choose to only implement a subset of the subtypes specified here. Clients must check what back-ends / processes actually support.", + "anyOf": [ + { + "$ref": "#/definitions/date-time" + }, + { + "$ref": "#/definitions/date" + }, + { + "$ref": "#/definitions/time" + }, + { + "$ref": "#/definitions/year" + }, + { + "type": "null" + } + ] + }, + "examples": [ + [ + "2015-01-01T00:00:00Z", + "2016-01-01T00:00:00Z" + ], + [ + "2015-01-01", + "2016-01-01" + ], + [ + "00:00:00Z", + "12:00:00Z" + ], + [ + "2015-01-01", + null + ] + ] + }, + "temporal-intervals": { + "type": "array", + "subtype": "temporal-intervals", + "title": "Multiple temporal intervals", + "description": "Left-closed temporal intervals, which are allowed to overlap. Formatted as array of two-element arrays, each being an array with subtype `temporal-interval`.", + "items": { + "$ref": "#/definitions/temporal-interval" + }, + "minItems": 1, + "examples": [ + [ + [ + "2015-01-01", + "2016-01-01" + ], + [ + "2016-01-01", + "2017-01-01" + ], + [ + "2017-01-01", + "2018-01-01" + ] + ], + [ + [ + "00:00:00Z", + "12:00:00Z" + ], + [ + "12:00:00Z", + "24:00:00Z" + ] + ], + [ + [ + "2020-01-01T00:00:00Z", + null + ] + ] + ] + }, + "time": { + "type": "string", + "subtype": "time", + "format": "time", + "title": "Time only", + "description": "Time only representation, as defined for `full-time` by [RFC 3339 in section 5.6](https://tools.ietf.org/html/rfc3339#section-5.6). Although [RFC 3339 prohibits the hour to be '24'](https://tools.ietf.org/html/rfc3339#section-5.7), this definition allows the value '24' for the hour as end time in an interval in order to make it possible that left-closed time intervals can fully cover the day." + }, + "udf-code": { + "type": "string", + "subtype": "udf-code", + "title": "UDF source code", + "description": "The (multi-line) source code of an user-defined function (UDF)." + }, + "udf-runtime": { + "type": "string", + "subtype": "udf-runtime", + "title": "UDF runtime", + "description": "The name of an UDF runtime." + }, + "udf-runtime-version": { + "type": "string", + "subtype": "udf-runtime-version", + "title": "UDF Runtime version", + "description": "The version of an UDF runtime." + }, + "uri": { + "type": "string", + "subtype": "uri", + "format": "uri", + "title": "URI", + "description": "A valid URI according to [RFC3986](https://tools.ietf.org/html/rfc3986)." + }, + "vector-cube": { + "type": "object", + "subtype": "vector-cube", + "title": "Vector data cube", + "description": "A vector data cube, a vector collection stored at the back-end. Different back-ends have different internal representations for this data structure" + }, + "wkt2-definition": { + "type": "string", + "subtype": "wkt2-definition", + "title": "WKT2 definition", + "description": "Specifies details about cartographic projections as WKT2 string. Refers to the latest WKT2 version (currently [WKT2:2018](http://docs.opengeospatial.org/is/18-010r7/18-010r7.html) / ISO 19162:2018) unless otherwise stated by the process." + }, + "year": { + "type": "string", + "subtype": "year", + "minLength": 4, + "maxLength": 4, + "pattern": "^\\d{4}$", + "title": "Year only", + "description": "Year representation, as defined for `date-fullyear` by [RFC 3339 in section 5.6](https://tools.ietf.org/html/rfc3339#section-5.6)." + } + } } \ No newline at end of file diff --git a/package.json b/package.json index 3c1ff3f..2016426 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "assets/subtype-schemas.json" ], "dependencies": { - "@openeo/js-commons": "^1.0.0", + "@openeo/js-commons": "^1.1.0", "ajv": "^6.12.0" }, "devDependencies": { diff --git a/src/jsonschema.js b/src/jsonschema.js index 624e326..c1216d5 100644 --- a/src/jsonschema.js +++ b/src/jsonschema.js @@ -1,5 +1,6 @@ const ajv = require('ajv'); const Utils = require('@openeo/js-commons/src/utils.js'); +const ProcessUtils = require('@openeo/js-commons/src/processUtils.js'); const keywords = require('./keywords'); var geoJsonSchema = require("../assets/GeoJSON.json"); @@ -82,12 +83,6 @@ module.exports = class JsonSchemaValidator { } } -/* validateSchema(schema) { - schema = this.makeSchema(schema); - let result = this.ajv.compile(schema); - return result.errors || []; - } */ - async validateSubtype(subtype, data, schema) { if (typeof subtypeSchemas.definitions[subtype] !== 'undefined') { schema = this.makeSchema(subtypeSchemas, true); @@ -219,10 +214,10 @@ module.exports = class JsonSchemaValidator { // So would a value compatible with valueSchema be accepted by paramSchema? // allowValueAsElements: If true, it checks whether the valueSchema would be allowed as part of an array or object. For example number could be allowed as part of an array of numbers. static isSchemaCompatible(paramSchema, valueSchema, strict = false, allowValueAsElements = false) { - var paramSchemas = JsonSchemaValidator.convertSchemaToArray(paramSchema); - var valueSchemas = JsonSchemaValidator.convertSchemaToArray(valueSchema); + var paramSchemas = ProcessUtils.normalizeJsonSchema(paramSchema, true); + var valueSchemas = ProcessUtils.normalizeJsonSchema(valueSchema, true); - var compatible = paramSchemas.filter(ps => { + var compatible = paramSchemas.findIndex(ps => { for(var i in valueSchemas) { var vs = valueSchemas[i]; if (typeof ps.type !== 'string' || (!strict && typeof vs.type !== 'string')) { // "any" type is always compatible @@ -257,47 +252,7 @@ module.exports = class JsonSchemaValidator { return false; }); - return compatible.length > 0; - } - - static convertSchemaToArray(originalSchema) { - var schemaArray = []; - if (!originalSchema || typeof originalSchema !== 'object') { - return schemaArray; - } - - // ToDo: schema.not and schema.allOf is not supported - see also class constructor of ProcessSchema in processSchema.js of openeo-web-editor. - if (Array.isArray(originalSchema.oneOf)) { - schemaArray = originalSchema.oneOf; - } - else if (Array.isArray(originalSchema.anyOf)) { - schemaArray = originalSchema.anyOf; - } - else if (!Array.isArray(originalSchema)) { - schemaArray = [originalSchema]; - } - else { - schemaArray = originalSchema; - } - - let splitMultiTypeSchemas = function(schema) { - return schema.type.map(type => Object.assign({}, schema, {type: type})); - }; - - let schemas = []; - for(let schema of schemaArray) { - if (!Utils.isObject(schema)) { - continue; - } - if (Array.isArray(schema.type)) { - schemas = schemas.concat(splitMultiTypeSchemas(schema)); - } - else { - schemas.push(schema); - } - } - - return schemas; + return compatible !== -1; } }; \ No newline at end of file diff --git a/src/node.js b/src/node.js index 82ecaa9..bb53b47 100644 --- a/src/node.js +++ b/src/node.js @@ -18,7 +18,7 @@ module.exports = class ProcessGraphNode { this.processGraph = parent; this.source = node; this.process_id = node.process_id; - this.arguments = Utils.isObject(node.arguments) ? JSON.parse(JSON.stringify(node.arguments)) : {}; + this.arguments = Utils.isObject(node.arguments) ? Utils.deepClone(node.arguments) : {}; this.description = node.description || null; this.isResultNode = node.result || false; this.expectsFrom = []; // From which node do we expect results from @@ -28,11 +28,7 @@ module.exports = class ProcessGraphNode { } toJSON() { - let args = {}; - for(var key in this.arguments) { - let arg = this.arguments[key]; - args[key] = Utils.isObject(arg) && typeof arg.toJSON === 'function' ? arg.toJSON() : arg; - } + let args = Utils.mapObjectValues(this.arguments, arg => Utils.isObject(arg) && typeof arg.toJSON === 'function' ? arg.toJSON() : arg); return Object.assign({}, this.source, { process_id: this.process_id, description: this.description, diff --git a/src/process.js b/src/process.js index cc4cb26..13d69fa 100644 --- a/src/process.js +++ b/src/process.js @@ -18,7 +18,7 @@ module.exports = class BaseProcess { async validate(node) { // Check for arguments we don't support and throw error - let unsupportedArgs = node.getArgumentNames().filter(name => this.parameters.filter(p => p.name === name) === 0); + let unsupportedArgs = node.getArgumentNames().filter(name => this.parameters.findIndex(p => p.name === name) === -1); if (unsupportedArgs.length > 0) { throw new ProcessGraphError('ProcessArgumentUnsupported', { process: this.id, diff --git a/src/processgraph.js b/src/processgraph.js index 13085b3..a51bedd 100644 --- a/src/processgraph.js +++ b/src/processgraph.js @@ -3,6 +3,7 @@ const JsonSchemaValidator = require('./jsonschema'); const ProcessGraphError = require('./error'); const ProcessGraphNode = require('./node'); const Utils = require('@openeo/js-commons/src/utils.js'); +const ProcessUtils = require('@openeo/js-commons/src/processUtils.js'); module.exports = class ProcessGraph { @@ -13,9 +14,6 @@ module.exports = class ProcessGraph { if (!Utils.isObject(this.process.process_graph)) { this.process.process_graph = {}; } - if (!Array.isArray(this.process.parameters)) { - this.process.parameters = []; - } this.processRegistry = processRegistry; this.jsonSchemaValidator = jsonSchemaValidator; this.allowEmptyGraph = false; @@ -122,9 +120,7 @@ module.exports = class ProcessGraph { } } - for(let id in this.process.process_graph) { - this.nodes[id] = this.createNodeInstance(this.process.process_graph[id], id, this); - } + this.nodes = Utils.mapObjectValues(this.process.process_graph, (pg, id) => this.createNodeInstance(pg, id, this)); for(let id in this.nodes) { var node = this.nodes[id]; @@ -259,7 +255,7 @@ module.exports = class ProcessGraph { args[argumentName] = this.createProcessGraph(arg, node, argumentName); break; case 'parameter': - if (!this.hasParameter(arg.from_parameter)) { + if (!this.hasParameter(arg.from_parameter) && !this.getCallbackParameter(arg.from_parameter)) { this.addParameter(arg.from_parameter); } break; @@ -280,10 +276,11 @@ module.exports = class ProcessGraph { } addParameter(name, description = '', schema = {}) { + if (!Array.isArray(this.process.parameters)) { + this.process.parameters = []; + } this.process.parameters.push({ - name: name, - description: description, - schema: schema + name, description, schema }); } @@ -304,15 +301,11 @@ module.exports = class ProcessGraph { } getParameters() { - return this.process.parameters; + return Array.isArray(this.process.parameters) ? this.process.parameters : []; } getParameter(name) { - let params = this.process.parameters.filter(p => p.name === name); - if (params.length > 0) { - return params[0]; - } - return null; + return this.getParameters().find(p => p.name === name) || null; } setArguments(args) { @@ -387,7 +380,7 @@ module.exports = class ProcessGraph { getProcess(node) { if (this.processRegistry === null) { - throw new Error("Process Registry not set."); + return null; } var process = this.processRegistry.get(node.process_id); if (process === null) { @@ -397,56 +390,26 @@ module.exports = class ProcessGraph { } getParentProcessId() { - if(this.node) { - return this.node.process_id; + if(this.getParentNode()) { + return this.getParentNode().process_id; } return null; } getParentProcess() { if (this.processRegistry === null) { - throw new Error("Process Registry not set."); + return null; } return this.processRegistry.get(this.getParentProcessId()); } getCallbackParameter(name) { let cbParams = this.getCallbackParameters(); - let result = cbParams.filter(p => p.name === name); - if (result.length === 1) { - return result[0]; - } - return null; + return cbParams.find(p => p.name === name) || null; } - getCallbackParameters() { - var process = this.getParentProcess(); - if (!this.parentParameterName || !Utils.isObject(process) || !Array.isArray(process.parameters)) { - return []; - } - - var schema = process.parameters.filter(p => p.name === this.parentParameterName); - if (schema.length === 1 && Array.isArray(schema[0].parameters)) { - return schema[0].parameters; - } - - // ToDo: If a process parameter supports multiple different callbacks, i.e. reduce with either an array of two separate values, this - // can't be separated accordingly and we just return all potential values. So it might happen that people get a successful validation - // but they used the wrong callback parameters. - // See issue https://github.com/Open-EO/openeo-js-processgraphs/issues/1 - - var cbParams = []; - var choice = Array.isArray(schema) ? schema : (schema.anyOf || schema.oneOf || schema.allOf); - if (Array.isArray(choice)) { - for(let i in choice) { - var p = choice[i]; - if (Array.isArray(p.parameters)) { - cbParams = cbParams.concat(p.parameters); - } - } - } - - return cbParams; + getCallbackParameters() { + return ProcessUtils.getCallbackParametersForProcess(this.getParentProcess(), this.parentParameterName); } }; \ No newline at end of file diff --git a/tests/jsonschema.test.js b/tests/jsonschema.test.js index 22a28e0..0ef536c 100644 --- a/tests/jsonschema.test.js +++ b/tests/jsonschema.test.js @@ -237,11 +237,6 @@ describe('JSON Schema Validator Tests', () => { var rasterCubeType = {type: "object", subtype: "raster-cube"}; var vectorCubeType = {type: "object", subtype: "vector-cube"}; var dataCubeType = [rasterCubeType, vectorCubeType]; - var load_collection_spatial_extent = [ - {"title":"Bounding Box","type":"object","subtype":"bounding-box","required":["west","south","east","north"],"properties":{"west":{"description":"West (lower left corner, coordinate axis 1).","type":"number"},"south":{"description":"South (lower left corner, coordinate axis 2).","type":"number"},"east":{"description":"East (upper right corner, coordinate axis 1).","type":"number"},"north":{"description":"North (upper right corner, coordinate axis 2).","type":"number"},"base":{"description":"Base (optional, lower left corner, coordinate axis 3).","type":["number","null"],"default":null},"height":{"description":"Height (optional, upper right corner, coordinate axis 3).","type":["number","null"],"default":null},"crs":{"description":"Coordinate reference system of the extent specified as EPSG code or PROJ definition. Whenever possible, it is recommended to use EPSG codes instead of PROJ definitions. Defaults to `4326` (EPSG code 4326) unless the client explicitly requests a different coordinate reference system.","schema":{"title":"EPSG Code","type":"integer","subtype":"epsg-code","examples":[3857],"default":4326}}}}, - {"title":"GeoJSON Polygon(s)","type":"object","subtype":"geojson"}, - {"type":"null"} - ]; test('isSchemaCompatible', async () => { expect(await JsonSchemaValidator.isSchemaCompatible(numberNullType, integerType)).toBeTruthy();