README
Datamatic
RxJS + JSON-Schema (Ajv) Based Observable and Validating Data Models and Pipelines
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 data evaluation and transformation
Table of Contents
Installation Instructions
$ npm install datamatic
UMD Usage (React, Angular, Vue et al)
import * as datamatic from "datamatic";
CommonJS Usage for NodeJS
const {Model, Pipeline} = require("datamatic");
DOM Window Usage
<script src="../../dist/datamatic.window.js"></script>
<script language="JavaScript">
const {Model, Pipeline} = window.datamatic;
</script>
Usage Examples
Basic Example
The example below defines a Model that expects a name
value and
list of topScores
items
const {Model} = require("datamatic");
// 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 Model({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 Model 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 model
// this will trigger the next notification
obj.model.topScores[0].score++;
// invalid attempt update the model
// this will trigger the error notification
// reason: "topScores/items/score" is type is integer
obj.model.topScores[0].score = "1234";
// invalid attempt update the model
// 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
Data Pipelines and Transformation
const {Pipeline} = require("datamatic");
/*
defines a schema that requires name, age and active attributes
filters out all items that do not conform to JSON-Schema below
*/
const schema = {
type: "object",
required: ["name", "age", "active"],
properties: {
name: {
$comment: "names must be in form: First Middle|MI Last",
type: "string",
pattern: "^[a-zA-Z]{1,24}\\s?[a-zA-Z]?\\s+[a-zA-Z]{1,24}