1.0.2 • Published 5 years ago

rxvo v1.0.2

Weekly downloads
-
License
-
Repository
github
Last release
5 years ago

RxVO

Reactive Validating Object RxJS + JSON-Schema (Ajv) Based Observable Data Models

Build Status Dependency Status Codacy Badge Codacy Badge Maintainability

Online Developer Documentation

Goals

  • Provide a means to quickly and easily validate complex data-sets
  • Look and feel like a standard JS Object for ease of use and adaptability
  • Automate creation of RxJS Update and Error notifications

Table of Contents

Installation Instructions

Usage Example

Developer Guide

Installation Instructions

$ npm i rxvo

Usage Example

The example below defines a Model that expects a name value and list of topScores items

// JSON-SCHEMA for Scores Collection
const schema = {
    "id": "root#",
    "type": "object",
    "properties": {
        "name": {
            "type": "string",
        },
        "topScores": {
            "type": "array",
            "minItems": 1,
            "maxItems": 3,
            "items": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string"
                    },
                    "score": {
                        "type": "integer",
                        "default": 0
                    }
                },
                "required": ["name"]
            }
        }
    },
    required: ["name", "topScores"],
};


// instantiate our Model
const obj = new RxVO({schemas: [schema]});

// subscribes an observer to the Model
obj.subscribe({
    next: function (ref) {
        console.log("\t>> update succeeded!\n\t%s\n\t%s\n\n",
            "current object state:", "" + JSON.stringify(ref));
    },
    complete: function (ref) {
        console.log("\t>> %s",
            "object is frozen and no longer editable");
    },
    error: function (e) {
        console.log("\t>> update FAILED with error:\n\t%s\n",
            JSON.stringify(e));
        console.log("\tcurrent object state:\n\t%s\n", obj);
    },
});

// populate the RxVO with data
// -- this will trigger the "next" notification
obj.model = {
    name: "JSONville",
    topScores: [{
        name: "Player 1",
        score: 12300000,
    }, {
        name: "Player 2",
        score: 45600000,
    }, {
        name: "Player 3",
        score: 78900000,
    }]
};

// update the rxVO
// this will trigger the next notification
obj.model.topScores[0].score++;

// invalid attempt update the rxVO
// this will trigger the error notification
// reason: "topScores/items/score" is type is integer 
obj.model.topScores[0].score = "1234";

// invalid attempt update the rxVO
// this will trigger the error notification
// reason: "topScores" is marked as required
delete obj.model.topScores;

Refer to the examples demo in ./examples/basic-usage for more usage examples

Developer Guide

RxVO Class

This class represents the Document entry point

MethodArgumentsDescription
constructorschemas config (object), options (object)creates new RxVO instance
errors getterretrieves errors (if any) from last json-schema validation
model getter/setterretrieves root model proxy object for operation
getModelsInPathto (string)retrieves models at given path
getSchemaForKeykey (string)retrieves json-schema with given key as ID
getSchemaForPathpath (string)retrieves json-schema for model at given path
schema getterretrieves json-schema for root model
subscribeobservers (object)Subscribes Observers to the RxVO Model Root
subscribeTopath (string), observers (object)Subscribes Observers to the Model at path
toStringretrieves root model as JSON String
toJSONretrieves root model as JSON Object
validatepath (string), value (object)validates data at given ath against JSON-Schema
static fromJSONjson (string | object)creates new RxVO from static method
RxVO Schemas Config
PropertyTypeDescription
metaarrayArray of MetaSchema references to validate Schemas against
schemasarrayArray of Schema references to validate data against
usestringkey/id of schema to use for data validation
Model Proxy Object

This is the Data Model most usage will be based around. It is a Proxy Object that has traps for all operations that alter the state of the given Array or Object

PropertyTypeDescription
$model(PropertiesModel | ItemsModel)references Proxy Object owner class
model vs $model

In usage, model always references the Proxied Data Model for validation and operation where $model references the owner Model Class

example:

 const _rxvo = new RxVO({schemas: [schema]});
 
 // access the root model:
 console.log(`JSON.stringify(_rxvo.model)`);
 
 // access the model's owner Model Class:
 const owner = _rxvo.model.$model;
 console.log(`is frozen: ${owner.isFrozen}`);
 
 // call toString on Owner
 console.log(`stringified: ${owner}`);
 
 // obtain model from  it's Owner
  console.log(`stringified: ${JSON.stringify(owner.model)}`);
 

ItemsModel

subclass of Model Class

Represents an Items (Array) entry in the given schema Note: the model param presents a Proxied Array, with all Array.prototype methods trapped and validatable

MethodArgumentsDescription
model getter/settersetter/getter for model proxy object for operation

PropertiesModel

subclass of Model Class

Represents an Properties (Object} entry in the given schema

MethodArgumentsDescription
getkey (string)applies Object.freeze to model hierarchy
model getter/settersetter/getter for model proxy object for operation
setkey (string), value (any)applies Object.freeze to model hierarchy

Model Class

MethodArgumentsDescription
freezeapplies Object.freeze to model hierarchy
isDirty getterreturns dirtyness of model heirarchy (is dirty if operation in progress)
isFrozen getterreturns Object.freeze status of Model hierarchy
jsonPath getterretrieves json path string for Model instance. eg: "this.is.my.path"
model getter/setterretrieves root model for operation
subscribeobservers (object)Subscribes Observers to the RxVO Model Root
subscribeTopath (string), observers (object)Subscribes Observers to the Model at path
model getter/settersetter/getter for model proxy object for operation
objectID getterretrieves Unique ObjectID of Model instance
options getterretrieves options passed to Model instance
path getterretrieves json-schema path string for Model instance. eg: "#/this/is/my/path"
parent getterretrieves Model's parent Model instance
resetresets model to initial state if operation is valid
root getterretrieves root Model instance
rxvo getterretrieves Model's RxVO document instance
toStringretrieves root model as JSON String
toJSONretrieves root model as JSON Object
validatepath (string), value (object)validates data at given ath against JSON-Schema
validationPath getterretrieves json-schema path string for Model validation