Validation of input parameters through JSON Schema

Registered by Dongdong Pei

Nova, Cinder and Keystone have used JSON Schema to validate input parameters in start of operation itself instead of going ahead and reported error after error occurs.

We should follow same in Manila.

Nova JSON Schema Validation::
## Validation:

    def validate(self, *args, **kwargs):
        try:
            self.validator.validate(*args, **kwargs)
        except jsonschema.ValidationError as ex:
            # NOTE: For whole OpenStack message consistency, this error
            # message has been written as the similar format of WSME.
            if len(ex.path) > 0:
                detail = _("Invalid input for field/attribute %(path)s."
                           " Value: %(value)s. %(message)s") % {
                               'path': ex.path.pop(), 'value': ex.instance,
                               'message': ex.message
                           }
            else:
                detail = ex.message
            raise exception.ValidationError(detail=detail)
        except TypeError as ex:
            # NOTE: If passing non string value to patternProperties parameter,
            # TypeError happens. Here is for catching the TypeError.
            detail = six.text_type(ex)
            raise exception.ValidationError(detail=detail)

## At API layer:
https://github.com/openstack/nova/blob/master/nova/api/openstack/compute/create_backup.py

    @validation.schema(create_backup.create_backup, '2.1')
    def _create_backup(self, req, id, body):
        """Backup a server instance.

https://github.com/openstack/nova/tree/master/nova/api/openstack/compute/schemas

Cinder JSON Schema Validation:
[1] https://blueprints.launchpad.net/cinder/+spec/json-schema-validation
[2] https://review.openstack.org/#/c/507386/

Blueprint information

Status:
Started
Approver:
Goutham Pacha Ravi
Priority:
Low
Drafter:
Dongdong Pei
Direction:
Approved
Assignee:
Stephen Finucane
Definition:
Approved
Series goal:
None
Implementation:
Started
Milestone target:
milestone icon dalmatian-rc1
Started by
Goutham Pacha Ravi

Related branches

Sprints

Whiteboard

Gerrit topic: https://review.openstack.org/#q,topic:bp/json-schema-validation,n,z

Addressed by: https://review.openstack.org/553662
    Add json schema validation for Manila APIs

Gerrit topic: https://review.opendev.org/#/q/topic:bp/json-schema-validation

Addressed by: https://review.opendev.org/c/openstack/manila/+/917153
    api: Add validation middleware

Addressed by: https://review.opendev.org/c/openstack/manila/+/917154
    api: Add schemas for resource_locks

Addressed by: https://review.opendev.org/c/openstack/manila/+/917155
    api: Add descriptions to API schemas

Addressed by: https://review.opendev.org/c/openstack/manila/+/917156
    tests: Ensure APIs have schemas

Addressed by: https://review.opendev.org/c/openstack/manila/+/917157
    tests: Ensure API schemas are actually valid

Addressed by: https://review.opendev.org/c/openstack/manila/+/921757
    api: Add schemas for availability_zones

Addressed by: https://review.opendev.org/c/openstack/manila/+/922333
    api: Add rst->md conversion

Gerrit topic: https://review.opendev.org/#/q/topic:openapi-schema-messages

Addressed by: https://review.opendev.org/c/openstack/manila/+/926050
    api: Add schemas for messages

Addressed by: https://review.opendev.org/c/openstack/manila/+/922566
    api: Add schemas for messages

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.

Subscribers

No subscribers.