npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@exocet/pandora-validation

v0.0.1-alpha.0

Published

Pandora data validation

Downloads

2

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