@exocet/pandora-validation
v0.0.1-alpha.0
Published
Pandora data validation
Downloads
1
Readme
Pandora Validation
Addon to provide flow and setup functions to validate data using JSON Schema using ajv.
npm install --save @exocet/pandora-validation
Concepts
Labels
Labels allow you to create variations of an entity's representation, they group fields and validations. When you do not specify a label for a field by default the label "default" will be used.
kind: Pandora/entity
metadata:
namespace: myNamespace
name: myEntity
spec:
validations: # Validations applied to entity instance
default: # Label
required:
- name
- uri
partial: # Label
required:
- id
- uri
reference: # Label
required:
- id
fields:
- name: id
labels: # This field is present only on "partial" and "reference" schema variations
- partial
- reference
- name: uri
labels:
- default
- partial
- name: name
type: string
labels:
- default
- partial
validation:
minLength: 1
maxLength: 80
- name: description # This field is present only on "default" schema variation
type: string
translated: true # Translated Pandora standard
validation:
nullable: true # Allow field to be null
maxLength: 500
Linked entities
Linked entities allow you to create references to structures and / or instances of other entities. They are similar to foreign keys, except that attributes are imported through denormalization. They can be linked references or not, when linked include the fields "id" and "uri", when not linked only import the total or partial structure of another entity.
kind: Pandora/entity
metadata:
namespace: myNamespace
name: myForeignEntity
spec:
fields:
- name: title
type: string
- name: description
type: string
- name: tags
type: array
validation:
uniqueItems: true
items:
- type: string
validation:
maxLength: 20
---
kind: Pandora/entity
metadata:
namespace: myNamespace
name: myEntity
spec:
fields:
- name: myObjectLinkedReference
uris: # Allowed URIs
- myNamespace.myForeignEntity
reference: myNamespace.myForeignEntity
linked: true # Linked reference, acts like a foreign key
- name: myObjectReference
uris:
- myNamespace.myForeignEntity
reference: myNamespace.myForeignEntity
linked: false # Unlinked reference, only imports the validations
required: # Cherry-pick the fields from the referenced entity
- title
- name: myArrayReference
type: array
items:
uris:
- myNamespace.myForeignEntity
reference: myNamespace.myForeignEntity
linked: false
required:
- title
- tags
Internal fields
Internal fields are ideal for fields that exist outside of a public scope, but are still within the structure of your entity. When you remove unknown properties from a schema normally that field would be removed, to avoid this behavior you can mark the fields as internal.
kind: Pandora/entity
metadata:
namespace: myNamespace
name: myEntity
spec:
fields:
- name: myInternalField
type: string
internal: true
Setup
Available features:
- Generate JSON Schemas from entity definition
- Multiple schemas defined by labels
- Provide validator hook
- Flow step: validation
To add this addon into your project, put the addon spec into your endpoint YAML:
kind: Pandora/endpoint
metadata:
name: myEndpoint
spec:
addons:
- name: validation
package: "@exocet/pandora-validation"
flow: true
setup: true
configuration:
validation: # Any configuration here will be passed to ajv configuration
removeAdditional: true
allErrors: true
After the setup the following property of the context will be available:
.application.ajv
- The instance of ajv
Hooks
The hooks created by this addon are:
- jsonSchemaValidator (
.service.hooks.useHook('jsonSchemaValidator')
) - Synchronous hook to validate data against entity schema.const [jsonSchemaValidator] = service.hooks.useHook('jsonSchemaValidator'); const errors = jsonSchemaValidator('myNamespace.myEntity/myLabel', data); // errors is null if the data is valid or an array with errors (as strings) if the data is invalid
Flow
The provided flow type and steps are listed bellow:
- validation - This flow validates data:
kind: Pandora/flowStep metadata: name: entityValidation spec: type: validation options: inboundFrom: request.data # JSON Path to acquire data from execution context outboundTo: null # JSON path to put the errors array if invalid entity: label: default namespace: stock name: product