mongoose-ajv-plugin
Advanced tools
Comparing version 1.0.0 to 1.0.1
49
index.js
@@ -0,1 +1,7 @@ | ||
// -------------------------------------------------------------------------------- | ||
// Programmer: Jason Thorpe | ||
// Language: typescript | ||
// Purpose: | ||
// Comments: | ||
// -------------------------------------------------------------------------------- | ||
"use strict"; | ||
@@ -27,27 +33,32 @@ /// <reference path="./typings/node/node.d.ts" /> | ||
schema.post('validate', function (data, next) { | ||
// APPLY THE OVERALL DOCUMENT SCHEMA | ||
if (SCHEMA && !SCHEMA(data)) { | ||
var error = new ValidationError(data); | ||
error.message += "; instance data does not match the JSON-schema"; | ||
error.errors.record = new ValidatorError('record', 'Overall object does not match JSON-schema', 'notvalid', data); | ||
error.errors.record.errors = SCHEMA.errors; | ||
return next(error); | ||
} | ||
// APPLY THE ATTRIBUTE SCHEMA | ||
for (var key in schemata) { | ||
if (data[key] === undefined) { | ||
// use the existing `required` validator for validating the presence of the attribute | ||
return; | ||
} | ||
if (!schemata[key](data[key])) { | ||
try { | ||
// APPLY THE OVERALL DOCUMENT SCHEMA | ||
if (SCHEMA && !SCHEMA(data)) { | ||
var error = new ValidationError(data); | ||
error.message += "; '" + key + "' attribute does not match it's JSON-schema"; | ||
error.errors[key] = new ValidatorError(key, key + ' does not match JSON-schema', 'notvalid', data); | ||
error.errors[key].errors = schemata[key].errors; | ||
error.message += "; instance data does not match the JSON-schema"; | ||
error.errors.record = new ValidatorError('record', 'Overall object does not match JSON-schema', 'notvalid', data); | ||
error.errors.record.errors = SCHEMA.errors; | ||
return next(error); | ||
} | ||
// APPLY THE ATTRIBUTE SCHEMA | ||
for (var key in schemata) { | ||
if (data[key] === undefined) { | ||
// use the existing `required` validator for validating the presence of the attribute | ||
return; | ||
} | ||
if (!schemata[key](data[key])) { | ||
var error = new ValidationError(data); | ||
error.message += "; '" + key + "' attribute does not match it's JSON-schema"; | ||
error.errors[key] = new ValidatorError(key, key + ' does not match JSON-schema', 'notvalid', data); | ||
error.errors[key].errors = schemata[key].errors; | ||
return next(error); | ||
} | ||
} | ||
next(); | ||
} | ||
next(); | ||
catch (err) { | ||
next(err); | ||
} | ||
}); | ||
} | ||
module.exports = ajv_plugin; |
57
index.ts
@@ -0,1 +1,8 @@ | ||
// -------------------------------------------------------------------------------- | ||
// Programmer: Jason Thorpe | ||
// Language: typescript | ||
// Purpose: | ||
// Comments: | ||
// -------------------------------------------------------------------------------- | ||
/// <reference path="./typings/node/node.d.ts" /> | ||
@@ -33,25 +40,29 @@ | ||
schema.post('validate', function (data,next) { | ||
// APPLY THE OVERALL DOCUMENT SCHEMA | ||
if(SCHEMA && !SCHEMA(data)){ | ||
var error = new ValidationError(data); | ||
error.message += `; instance data does not match the JSON-schema`; | ||
error.errors.record = new ValidatorError('record', 'Overall object does not match JSON-schema', 'notvalid', data); | ||
error.errors.record.errors = SCHEMA.errors; | ||
return next(error); | ||
} | ||
// APPLY THE ATTRIBUTE SCHEMA | ||
for(var key in schemata){ | ||
if(data[key] === undefined){ | ||
// use the existing `required` validator for validating the presence of the attribute | ||
return; | ||
} | ||
if(!schemata[key](data[key])){ | ||
var error = new ValidationError(data); | ||
error.message += `; '${key}' attribute does not match it's JSON-schema`; | ||
error.errors[key] = new ValidatorError(key, key+' does not match JSON-schema', 'notvalid', data); | ||
error.errors[key].errors = schemata[key].errors; | ||
return next(error); | ||
} | ||
} | ||
next(); | ||
try{ | ||
// APPLY THE OVERALL DOCUMENT SCHEMA | ||
if(SCHEMA && !SCHEMA(data)){ | ||
var error = new ValidationError(data); | ||
error.message += `; instance data does not match the JSON-schema`; | ||
error.errors.record = new ValidatorError('record', 'Overall object does not match JSON-schema', 'notvalid', data); | ||
error.errors.record.errors = SCHEMA.errors; | ||
return next(error); | ||
} | ||
// APPLY THE ATTRIBUTE SCHEMA | ||
for(var key in schemata){ | ||
if(data[key] === undefined){ | ||
// use the existing `required` validator for validating the presence of the attribute | ||
return; | ||
} | ||
if(!schemata[key](data[key])){ | ||
var error = new ValidationError(data); | ||
error.message += `; '${key}' attribute does not match it's JSON-schema`; | ||
error.errors[key] = new ValidatorError(key, key+' does not match JSON-schema', 'notvalid', data); | ||
error.errors[key].errors = schemata[key].errors; | ||
return next(error); | ||
} | ||
} | ||
next(); | ||
}catch(err){ | ||
next(err); | ||
} | ||
}) | ||
@@ -58,0 +69,0 @@ |
{ | ||
"name": "mongoose-ajv-plugin", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "AJV plugin for Mongoose", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -30,3 +30,2 @@ | ||
```JavaScript | ||
var mongoose = require('mongoose'); | ||
@@ -36,3 +35,2 @@ var Schema = mongoose.Schema; | ||
mongoose.Promise = require('bluebird'); | ||
``` | ||
@@ -48,7 +46,5 @@ | ||
Define a JSON-schema for your favorite attribute: | ||
```JavaScript | ||
var contact_json_schema = { | ||
@@ -86,3 +82,2 @@ "type":"object", | ||
}; | ||
``` | ||
@@ -93,3 +88,2 @@ | ||
```JavaScript | ||
// use AJV to validate fields within a document | ||
@@ -111,3 +105,2 @@ var Player_schema = new Schema({ | ||
}); | ||
``` | ||
@@ -120,3 +113,2 @@ If you didn't load the mongoose-ajv-plugin globally , you'll need to add it to your schema now: | ||
Player_schema.plugin(ajv_plugin); | ||
``` | ||
@@ -158,4 +150,3 @@ | ||
validate_promise(oscar,"Oscar") // promise based validation * | ||
>> Oscar failed validation with message: Player validation failed; 'contact' attribute does not match it's JSON-schema ** | ||
>> Oscar failed validation with message: Player validation failed; 'contact' attribute does not match it's JSON-schema | ||
``` | ||
@@ -171,3 +162,2 @@ \* see `convenience functions` section below. | ||
```JavaScript | ||
var team_json_schema = { | ||
@@ -192,3 +182,2 @@ "type":"object", | ||
}; | ||
``` | ||
@@ -202,3 +191,2 @@ | ||
```JavaScript | ||
var Team_schema = new Schema({ | ||
@@ -210,3 +198,2 @@ team_name: String, | ||
var Team = mongoose.model('Team', Team_schema); | ||
``` | ||
@@ -217,3 +204,2 @@ | ||
```JavaScript | ||
var just_me = new Team({ | ||
@@ -259,3 +245,2 @@ "team_name": "Just Me", | ||
```JavaScript | ||
var AJV = require('ajv'), | ||
@@ -265,3 +250,2 @@ ajv = new AJV(); | ||
Team_schema.plugin(ajv_plugin,{schema: team_json_schema,ajv: ajv}); | ||
``` | ||
@@ -268,0 +252,0 @@ |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 5 instances in 1 package
270161
5423
1
17
261