Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
switch (true) {
// required
case schema[key].required && !obj.hasOwnProperty(key):
httpAssert(false, 500, `\`${key}\` is required`);
// check type
case obj.hasOwnProperty(key) && !Helper.compareType(schema[key].type(), obj[key]):
httpAssert(false, 500, `\`${key}\` should be a ${Helper.is(schema[key].type())}`);
// check length
case obj.hasOwnProperty(key) && schema[key].length:
checkLength(obj[key], schema[key].length, key);
// check pattern
case obj.hasOwnProperty(key) && schema[key].pattern:
httpAssert(schema[key].pattern.test(obj[key]), 500, schema[key].message || `\`${key}\` is not valid`);
// default value
case !obj.hasOwnProperty(key) && schema[key].hasOwnProperty('default'):
obj[key] = schema[key].default;
}
return {key: key, value: obj[key]};
}
export function validSchema(key, obj, schema) {
// ignore
if(!schema.hasOwnProperty(key)) return null;
switch (true) {
// required
case schema[key].required && !obj.hasOwnProperty(key):
httpAssert(false, 500, `\`${key}\` is required`);
// check type
case obj.hasOwnProperty(key) && !Helper.compareType(schema[key].type(), obj[key]):
httpAssert(false, 500, `\`${key}\` should be a ${Helper.is(schema[key].type())}`);
// check length
case obj.hasOwnProperty(key) && schema[key].length:
checkLength(obj[key], schema[key].length, key);
// check pattern
case obj.hasOwnProperty(key) && schema[key].pattern:
httpAssert(schema[key].pattern.test(obj[key]), 500, schema[key].message || `\`${key}\` is not valid`);
// default value
case !obj.hasOwnProperty(key) && schema[key].hasOwnProperty('default'):
obj[key] = schema[key].default;
export function collection(ctx, collectionName) {
var db = ctx.app.context.db;
httpAssert(db, 503);
return db.collection(collectionName);
};