Comparing version 2.2.0 to 2.3.0
@@ -33,5 +33,7 @@ { | ||
}, | ||
"eslint.autoFixOnSave": true, | ||
"eslint.alwaysShowStatus": true, | ||
"eslint.run": "onType" | ||
"eslint.run": "onType", | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll.eslint": true | ||
} | ||
} |
{ | ||
"name": "rdb", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"main": "index.js", | ||
@@ -24,2 +24,3 @@ "title": "rdb", | ||
"dependencies": { | ||
"ajv": "^6.10.2", | ||
"deferred": "^0.7.5", | ||
@@ -26,0 +27,0 @@ "mysql": "^2.13.0", |
@@ -18,2 +18,4 @@ __rdb__ | ||
## Release notes | ||
__2.3.0__ | ||
Added column validation with JSON Schema or plain validator. | ||
__2.2.0__ | ||
@@ -20,0 +22,0 @@ Throw meaningful error when getById gives no match. |
@@ -0,1 +1,4 @@ | ||
let Ajv = require('ajv'); | ||
let inspect = require('util').inspect; | ||
function defineColumn(column, table) { | ||
@@ -62,2 +65,23 @@ var c = {}; | ||
c.validate = function(value) { | ||
column.validate = value; | ||
return c; | ||
}; | ||
c.JSONSchema = function(schema, options) { | ||
let ajv = new Ajv(options); | ||
let validate = ajv.compile(schema); | ||
column.validate = _validate; | ||
function _validate(value) { | ||
let valid = validate(value); | ||
if (!valid) { | ||
let e = new Error(`Column ${table._dbName}.${column._dbName} violates JSON Schema: ${inspect(validate.errors, false, 10)}`); | ||
e.errors = validate.errors; | ||
throw e; | ||
} | ||
} | ||
return c; | ||
}; | ||
return c; | ||
@@ -64,0 +88,0 @@ } |
@@ -43,2 +43,4 @@ var updateField = require('../updateField'); | ||
this._dbRow[key] = value; | ||
if (column.validate) | ||
column.validate(value, this._dbRow); | ||
updateField(table, column, this); | ||
@@ -45,0 +47,0 @@ var emit = this._emitColumnChanged[name]; |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
256902
5712
202
9
+ Addedajv@^6.10.2
+ Addedajv@6.12.6(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addeduri-js@4.4.1(transitive)