Issues and Decisions
Overview
Currently when persisting any data in CPS the data validation is performed using the yang schema and upon successful validation the data is stored in CPS DB. But in case the data validation fails a 500 response is returned by CPS.
So, in case the data fails the validation check there is no way for the user to know the exact cause of failure. The failure is logged and can be seen in the container logs:
This is not very helpful for the user as it does not provide proper information about the root cause of failure.
Proposal
The idea here is to have a mechanism to validate the data and throw an appropriate response with message, before the data is persisted in the database. This validation check is not meant to be limited for new data but should also extend to sub-parts of a larger data set, such as data used in case of an update operation. So based on this the following approaches are proposed to perform a validation on data:
- A new endpoint to validate the JSON payload against an existing schema in CPS DB, this endpoint will only validate the data and never persist it.
- A dry-run flag in the existing CPS API's which would validate the data and return a response containing the details of validation failure. This flag can be implemented to multiple CPS APIs such as POST (Create a Node), PATCH (Update a Node) and PUT (Replace a node), where we can validate the entire data or parts of data and return the exact point of failure in the validation as part of response.
Implementation Ideas
In either case the endpoint should return a REST response with appropriate code and message.
- Response Code: 400
- Response Body: a JSON response providing details about the failure. (response body format to be finalized)
List of impacted APIs
Name | Endpoint | Issue | Required Action |
---|---|---|---|
Create a Node | /{apiVersion}/dataspaces/{dataspace-name}/anchors/{anchor-name}/nodes | Returns 500 status on data validation failure | Return 400 status with updated and detailed error message. Updated Message: { "status": "400 BAD_REQUEST", "message": "Data Validation Failed", "details": "Failed to parse JSON data. Data node with name "Node" was not found under Schema Node "categories"." } |
Update Node Leaves | /{apiVersion}/dataspaces/{dataspace-name}/anchors/{anchor-name}/nodes | Returns 400 status with entire JSON payload in error message | |
Replace Nodes | /{apiVersion}/dataspaces/{dataspace-name}/anchors/{anchor-name}/nodes | Returns 400 status with entire JSON payload in error message | |
Add List Element | /{apiVersion}/dataspaces/{dataspace-name}/anchors/{anchor-name}/list-nodes | Returns 500 status on data validation failure | |
Replace List element | /{apiVersion}/dataspaces/{dataspace-name}/anchors/{anchor-name}/list-nodes | Returns 400 status with entire JSON payload in error message | |
Get Delta between anchor and JSON payload | /v2/dataspaces/{dataspace-name}/anchors/{anchor-name}/deltaAnchors | Returns 400 status with entire JSON payload in error message |