New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mongo-schema

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongo-schema - npm Package Compare versions

Comparing version 0.1.22 to 0.1.24

266

lib/mongo-schema.js

@@ -33,2 +33,3 @@ var MongoSchema = function () {

var re;
var type;
for (var key in schema) {

@@ -57,5 +58,15 @@

}
re = self.check(value[i], properties[0], update);
newData[key][i] = re.data;
errors = errors.concat(re.errors);
if(typeof properties[0] === 'object' && properties[0].type === undefined){
re = self.check(value[i], properties[0], update);
newData[key][i] = re.data;
errors = errors.concat(re.errors);
} else {
console.log('check', value[i], properties[0]);
re = checkPrimitive(value[i], properties[0], {}, key, errors);
if(re !== undefined){
newData[key][i] = re;
}
}
}

@@ -88,123 +99,7 @@ } else if (!update) {

var normType = normalizeType(type);
if (!normType) {
throw new Error("type " + type + " not available");
var re = checkPrimitive(value, type, properties, key, errors);
if(re !== undefined){
newData[key] = re;
}
switch (normType) {
case 'string':
if ((!isString(value) && !isNumber(value)) || isNaN(value)) {
value = undefined;
if (properties.required) {
errors.push(key + " - required not set on '" + value + "'");
}
} else {
value = value + '';
if (!isUndefined(properties.minLength) && value.length < properties.minLength) {
if (properties.required) {
errors.push(key + " - minLength " + properties.minLength + " not fulfilled on '" + value + "'");
}
continue;
}
if (!isUndefined(properties.maxLength) && value.length > properties.maxLength) {
if (properties.required) {
errors.push(key + " - maxLength " + properties.regex + " not fulfilled on '" + value + "'");
}
value = value.substr(0, properties.maxLength);
}
if (!isUndefined(properties.regex) && !properties.regex.test(value)) {
if (properties.required) {
errors.push(key + " - regex " + properties.regex + " not fulfilled '" + value + "'");
}
continue;
}
}
if (!isUndefined(value)) {
newData[key] = value;
}
continue;
case 'number':
if (isArray(value) || isNull(value)) {
value = undefined;
}
value = value * 1;
if (isNaN(value)) {
value = undefined;
}
if (properties.required && isUndefined(value)) {
errors.push(key + " - required not set on '" + value + "'");
continue;
}
if (!isUndefined(value) && !isUndefined(properties.min) && value < properties.min) {
if (properties.required) {
errors.push(key + " - min " + properties.min + " not fulfilled '" + value + "'");
}
continue;
}
if (!isUndefined(value) && !isUndefined(properties.max) && value > properties.max) {
if (properties.required) {
errors.push(key + " - max " + properties.max + " not fulfilled on '" + value + "'");
}
continue;
}
if (!isUndefined(value)) {
newData[key] = value;
}
continue;
case 'boolean':
if (properties.required && isUndefined(value)) {
errors.push(key + " - required not set '" + value + "'");
continue;
} else if (isUndefined(value)) {
continue;
}
newData[key] = !!value;
continue;
case 'date':
if (value instanceof Date) {
value = Math.floor(value.getTime() / 1000);
}
value = value * 1;
if (isNaN(value)) {
value = undefined;
} else {
if (value.toString().length === 13) {
value = Math.floor(value / 1000);
}
if (value.toString().length !== 10) {
value = undefined;
}
}
if (properties.required && isUndefined(value)) {
errors.push(key + " - required not set on '" + value + "'");
continue;
}
if (!isUndefined(value)) {
newData[key] = value;
}
continue;
case 'objectid':
if (typeof value !== "string" || (typeof value == "string" && value.length !== 24)) {
value = undefined;
}
if (properties.required && isUndefined(value)) {
errors.push(key + " - required not set '" + value + "'");
continue;
}
if (!isUndefined(value)) {
newData[key] = value;
}
continue;
}
}

@@ -215,2 +110,5 @@ }

this.defs = function (schema) {

@@ -250,2 +148,126 @@ if (isArray(schema) && schema.length === 1) {

function checkPrimitive(value, type, properties, key, errors){
var normType = normalizeType(type);
if (!normType) {
throw new Error("type " + type + " not available");
}
switch (normType) {
case 'string':
if ((!isString(value) && !isNumber(value)) || isNaN(value)) {
value = undefined;
if (properties.required) {
errors.push(key + " - required not set on '" + value + "'");
}
} else {
value = value + '';
if (!isUndefined(properties.minLength) && value.length < properties.minLength) {
if (properties.required) {
errors.push(key + " - minLength " + properties.minLength + " not fulfilled on '" + value + "'");
}
return;
}
if (!isUndefined(properties.maxLength) && value.length > properties.maxLength) {
if (properties.required) {
errors.push(key + " - maxLength " + properties.regex + " not fulfilled on '" + value + "'");
}
value = value.substr(0, properties.maxLength);
}
if (!isUndefined(properties.regex) && !properties.regex.test(value)) {
if (properties.required) {
errors.push(key + " - regex " + properties.regex + " not fulfilled '" + value + "'");
}
return;
}
}
if (!isUndefined(value)) {
return value;
}
return;
case 'number':
if (isArray(value) || isNull(value)) {
value = undefined;
}
value = value * 1;
if (isNaN(value)) {
value = undefined;
}
if (properties.required && isUndefined(value)) {
errors.push(key + " - required not set on '" + value + "'");
return;
}
if (!isUndefined(value) && !isUndefined(properties.min) && value < properties.min) {
if (properties.required) {
errors.push(key + " - min " + properties.min + " not fulfilled '" + value + "'");
}
return;
}
if (!isUndefined(value) && !isUndefined(properties.max) && value > properties.max) {
if (properties.required) {
errors.push(key + " - max " + properties.max + " not fulfilled on '" + value + "'");
}
return;
}
if (!isUndefined(value)) {
return value;
}
return;
case 'boolean':
if (properties.required && isUndefined(value)) {
errors.push(key + " - required not set '" + value + "'");
return;
} else if (isUndefined(value)) {
return;
}
return !!value;
return;
case 'date':
if (value instanceof Date) {
value = Math.floor(value.getTime() / 1000);
}
value = value * 1;
if (isNaN(value)) {
value = undefined;
} else {
if (value.toString().length === 13) {
value = Math.floor(value / 1000);
}
if (value.toString().length !== 10) {
value = undefined;
}
}
if (properties.required && isUndefined(value)) {
errors.push(key + " - required not set on '" + value + "'");
return;
}
if (!isUndefined(value)) {
return value;
}
return;
case 'objectid':
if (typeof value !== "string" || (typeof value == "string" && value.length !== 24)) {
value = undefined;
}
if (properties.required && isUndefined(value)) {
errors.push(key + " - required not set '" + value + "'");
return;
}
if (!isUndefined(value)) {
return value;
}
return;
}
}
function getDef(prop) {

@@ -252,0 +274,0 @@ if (isObject(prop) && !isNull(prop) && !isUndefined(prop.def)) {

{
"name": "mongo-schema",
"version": "0.1.22",
"version": "0.1.24",
"main": "lib/mongo-schema.js",

@@ -5,0 +5,0 @@ "description": "Basic schema for mongodb",

@@ -48,12 +48,12 @@ # mongo-schema [![Build Status](https://secure.travis-ci.org/paul-em/mongo-schema.png?branch=master)](http://travis-ci.org/paul-em/mongo-schema)

var numberSchema = {
successTestNumber1: Number,
successTestNumber2: Number,
convertTestNumber: Number,
failTestNumber1: Number,
failTestNumber2: Number,
failTestNumber3: Number,
failTestNumber4: Number,
failTestNumber5: Number,
failTestNumber6: Number,
failTestNumber7: Number
successTestNumber1: "Number",
successTestNumber2: "Number",
convertTestNumber: "Number",
failTestNumber1: "Number",
failTestNumber2: "Number",
failTestNumber3: "Number",
failTestNumber4: "Number",
failTestNumber5: "Number",
failTestNumber6: "Number",
failTestNumber7: "Number"
};

@@ -60,0 +60,0 @@

@@ -710,1 +710,28 @@ var schema = require("../lib/mongo-schema.js");

});
var updateSchemaArray = {
a: [
'Number'
]
};
var updateSchemaArrayData = {
a: [1,2,3,4,5]
};
describe("updateSchemaCollection", function () {
it("should ignore wrong type and return no errors", function (done) {
var re = schema.check(updateSchemaArrayData, updateSchemaArray, true);
var data = re.data;
var errs = re.errors;
console.log(data);
assert.strictEqual(errs.length, 0);
assert.strictEqual(typeof data.a, "object");
assert.strictEqual(data.a.length, 5);
assert.strictEqual(typeof data.a[0], "number");
assert.strictEqual(data.a[0], 1);
assert.strictEqual(data.a[4], 5);
done();
})
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc