simple_validator
Advanced tools
| { | ||
| "$schema": "http://json-schema.org/draft-04/schema#", | ||
| "id": "https://switchpaas.rocks/schema.json#test_simple_validator_1", | ||
| "title" : "A simple json schema for testing validation", | ||
| "type" : "object", | ||
| "properties": { | ||
| "aProperty": { | ||
| "type": "integer", | ||
| "minimum": 1, | ||
| "exclusiveMinimum": true | ||
| } | ||
| } | ||
| } |
+2
-1
| { | ||
| "name": "simple_validator", | ||
| "description": "Basic building bricks for validations", | ||
| "version": "0.0.1", | ||
| "version": "0.0.2", | ||
| "keywords": [ | ||
@@ -30,2 +30,3 @@ "validator", | ||
| "dependencies": { | ||
| "jsonschema": "1.1.1" | ||
| }, | ||
@@ -32,0 +33,0 @@ "devDependencies": { |
+21
-0
@@ -7,3 +7,24 @@ /* | ||
| 'use strict'; | ||
| var schemaFiles = {}; | ||
| var jsonschema = require('jsonschema'); | ||
| var fs = require('fs'); | ||
| exports.validateWithSchema = function (doc, schemaFile, schemaName) { | ||
| var schemaObject; | ||
| if (!schemaFiles[schemaFile]) { | ||
| var schemaString = fs.readFileSync(schemaFile).toString('utf8'); | ||
| schemaFiles[schemaFile] = JSON.parse(schemaString); | ||
| } | ||
| schemaObject = schemaFiles[schemaFile]; | ||
| var v = new jsonschema.Validator(); | ||
| v.addSchema(schemaObject, schemaName); | ||
| return v.validate(doc, schemaObject).errors.map(function (e) { | ||
| var prop = e.property.split('.'); | ||
| prop.shift(); | ||
| prop = prop.join(''); | ||
| return [prop, e.message].join(' '); | ||
| }); | ||
| }; | ||
| exports.hasProperty = function (object, property) { | ||
@@ -10,0 +31,0 @@ return object[property] !== undefined; |
+14
-0
@@ -11,2 +11,16 @@ /* | ||
| describe('Validator', function () { | ||
| describe('JSON schemas', function () { | ||
| assert.equal( | ||
| m.validateWithSchema( | ||
| {aProperty: 2}, __dirname + '/resources/schema.json', 'testSchema' | ||
| ).length, | ||
| 0 | ||
| ); | ||
| assert.deepEqual( | ||
| m.validateWithSchema( | ||
| {aProperty: 1}, __dirname + '/resources/schema.json', 'testSchema' | ||
| ), | ||
| ['aProperty must have a minimum value of 1'] | ||
| ); | ||
| }); | ||
| describe('Numbers', function () { | ||
@@ -13,0 +27,0 @@ it('integer ranges (including boundaries)', function () { |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
20182
7.5%10
11.11%176
34.35%1
Infinity%1
Infinity%+ Added
+ Added