Schemaly
This library is designed to help work with data objects by providing structure, validation, sanitization and policy driven access control. Situations where you may need this functionality include retrieving data from a database and restricting what the output should contain using user roles and scope. Processing request data from an unknown source and stripping out unexpected or unauthorised values. Creating universal data models to share on your client and server side projects to allow for shared sanitization + validation. A mixture of the previous examples to lock down and regulate data for serving, receiving and storing data.
Pretty much you can use this library to provide client side model and validation functionality and/or as a middleware to your node api REST or GraphQL endpoints.
Installation
Install by using npm
npm install --save schemaly
or by using yarn
yarn add schemaly
Quick Start
; // Create your data schema// This is a simple example with only one STRING field// You can create complex fields with child fields (CONTAINER) or multiple groups of child fields (COLLECTION)const profile = ; // Create a collider to handle applying data to the schemaconst collider = ; // Apply the data to the schema and dump the valuescollider ;
Concepts
Schemaly has a few concepts for handling schema structure and application of data to schema. There are a number of native classes which implement these concepts such as Field, Fields, Hydrate, Hydrated etc however you have the ability to create your own extensions.
The process basically has two parts. First, the outlining of schemas which describe data structure relationships, allowed values and permissions. The second, allows from external data to be injected into the schema to product a hydrated set of objects that have additional information such as sanitized values, validation results and policy driven stripped out data.
The act of applying data to the schema is called a collide and is handled by Interact classes.
- Model: Contains the schema structure in the form of Model instances and contains allowed roles and scope. You would typically have one Model representing one thing ie. User, Order etc
- Blueprint: Contains data about an individual field (ie a name, date of birth etc) and contains validation, sanitization and policy rules governing the field.
- Interact: Handles applying data objects to an Model schema and produces Effects.
- Effect: Created for each Blueprint that passes policy checks. An effect contains a hydrated value with the ability to view sanitized values and validation results.
Blueprint
Blueprint Policies
// Import on top of earlier dependencies etc; // ... wrapped in schema const collider = ; collider ;
Blueprint Sanitization
// Import on top of earlier dependencies etc; // ... wrapped in schema // ... Create collider etc collider // Sanitizers will automatically be applied during hydration ;
Blueprint Validation
// Import on top of earlier dependencies etc; // ... wrapped in schema // ... Create collider etc collider ;
Container Blueprints
// Import on top of earlier dependencies etc; // ... wrapped in schema // ... Create collider etc collider ;
Collection Blueprints
// Import on top of earlier dependencies etc; // ... wrapped in schema // ... Create collider etc collider ;