Forráskód Böngészése

readme and initial

Ivan Asmer 3 éve
commit
efcb2fbb88
5 módosított fájl, 179 hozzáadás és 0 törlés
  1. 31 0
      .gitignore
  2. 16 0
      README.md
  3. 62 0
      index.js
  4. 47 0
      package-lock.json
  5. 23 0
      package.json

+ 31 - 0
.gitignore

@@ -0,0 +1,31 @@
+# ---> Node
+# Logs
+logs
+*.log
+npm-debug.log*
+
+# Runtime data
+pids
+*.pid
+*.seed
+
+# Directory for instrumented libs generated by jscoverage/JSCover
+lib-cov
+
+# Coverage directory used by tools like istanbul
+coverage
+
+# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
+.grunt
+
+# node-waf configuration
+.lock-wscript
+
+# Compiled binary addons (http://nodejs.org/api/addons.html)
+build/Release
+
+# Dependency directory
+# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
+node_modules
+
+public/images

+ 16 - 0
README.md

@@ -0,0 +1,16 @@
+# Pretty Simple JSON-Schema Javascript Proxy validator.
+
+## API:
+
+```javascript
+const jsonSchemaProxy = require('json-schema-proxy')
+
+const proxiedObject = jsonSchemaProxy(objectToProxy, inputJsonSchema, outputJsonSchema)
+
+```
+`proxiedObject.foo = 'some value'` will cause exception in case of failed inSchema validation. Object remains untouched 
+`let variable      = proxiedObject.foo` will give undefined in case of failed outSchema validation.
+
+`outSchema` can be omitted if you don't need output validation.
+
+`outSchema` filters wrong fields or `additionalProperties` not listed in outSchema

+ 62 - 0
index.js

@@ -0,0 +1,62 @@
+const Ajv = require('ajv')
+
+
+module.exports = (obj={}, inSchema, outSchema) => {
+    if (!inSchema) throw new ReferenceError('input schema not defined')
+
+    const inValidate  = (new Ajv()).compile(inSchema)
+    let   outValidate;
+
+    if (outSchema){
+        outSchema                      = {...outSchema}
+        outSchema.additionalProperties = false
+        outValidate                    = (new Ajv()).compile(outSchema)
+    }
+
+    return new Proxy(obj, {
+        get(obj, prop){
+            if (outValidate && !outValidate(obj)){
+                if (outValidate.errors.find(({instancePath}) => instancePath.slice(1).startsWith(prop)) ||
+                    (outValidate.errors[0].keyword === 'additionalProperties' && outValidate.errors[0].params.additionalProperty === prop)){
+                    return;
+                }
+            }
+            return obj[prop]
+        },
+        set(obj, prop, value){
+            const newObject = {...obj, [prop]: value}
+            if (!inValidate(newObject)){
+                throw new TypeError(JSON.stringify([inValidate.errors, prop, value], null, 4))
+            }
+            obj[prop] = value
+        }
+    })
+}
+
+const schema = {
+    type: "object",
+    properties: {
+        foo: {type: "integer"},
+        bar: {type: "string"}
+    },
+    required: ["foo"],
+    additionalProperties: false
+}
+
+const outSchema = {
+    type: "object",
+    properties: {
+        foo: {type: "integer"},
+    },
+    required: ["foo"],
+    additionalProperties: false
+}
+
+let validated = module.exports({}, schema, outSchema)
+
+validated.foo = 5;
+validated.bar = "string";
+console.log(validated.foo)
+console.log(validated.bar)
+console.log({...validated})
+//validated.bar = 100500;

+ 47 - 0
package-lock.json

@@ -0,0 +1,47 @@
+{
+  "name": "json-schema-proxy",
+  "version": "0.0.1",
+  "lockfileVersion": 1,
+  "requires": true,
+  "dependencies": {
+    "ajv": {
+      "version": "8.4.0",
+      "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.4.0.tgz",
+      "integrity": "sha512-7QD2l6+KBSLwf+7MuYocbWvRPdOu63/trReTLu2KFwkgctnub1auoF+Y1WYcm09CTM7quuscrzqmASaLHC/K4Q==",
+      "requires": {
+        "fast-deep-equal": "^3.1.1",
+        "json-schema-traverse": "^1.0.0",
+        "require-from-string": "^2.0.2",
+        "uri-js": "^4.2.2"
+      }
+    },
+    "fast-deep-equal": {
+      "version": "3.1.3",
+      "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
+      "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q=="
+    },
+    "json-schema-traverse": {
+      "version": "1.0.0",
+      "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz",
+      "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug=="
+    },
+    "punycode": {
+      "version": "2.1.1",
+      "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz",
+      "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A=="
+    },
+    "require-from-string": {
+      "version": "2.0.2",
+      "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz",
+      "integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw=="
+    },
+    "uri-js": {
+      "version": "4.4.1",
+      "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz",
+      "integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==",
+      "requires": {
+        "punycode": "^2.1.0"
+      }
+    }
+  }
+}

+ 23 - 0
package.json

@@ -0,0 +1,23 @@
+{
+  "name": "json-schema-proxy",
+  "version": "0.0.1",
+  "description": "JSON-Schema Proxy for validation any object on read and write",
+  "main": "index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "repository": {
+    "type": "git",
+    "url": "http://gitlab.a-level.com.ua/gitgod/json-schema-proxy.git"
+  },
+  "keywords": [
+    "ajv",
+    "proxy",
+    "json-schema"
+  ],
+  "author": "Ivan Grynkin",
+  "license": "GPL-3.0-or-later",
+  "dependencies": {
+    "ajv": "^8.4.0"
+  }
+}