Skip to content

Commit

Permalink
check validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Orit Persik authored and Avraham Ostreicher committed Jun 10, 2019
1 parent 156f24b commit 1ed9da6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
8 changes: 5 additions & 3 deletions server/controllers/schema.controller.js
Expand Up @@ -3,6 +3,8 @@ const fs = require('fs');
const IncomingForm = require('formidable').IncomingForm;
const entitiesCtrl = require('./entity.controller');

const updateOpts = { new: true, runValidators: true };

module.exports = {
insert,
update,
Expand All @@ -26,7 +28,7 @@ async function upload(request) {
schema.updated = new Date();
resolve(Schema.findOneAndUpdate({
category: schema.category
}, schema, {upsert : true}))
}, schema, {upsert : true, runValidators: true}))
});
form.parse(request);
});
Expand All @@ -35,8 +37,8 @@ async function upload(request) {
async function update(name, schema) {
schema.updated = new Date();
return await Schema.findOneAndUpdate({
name: name
}, schema);
category: category
}, schema, updateOpts);
}

async function remove(schemaId) {
Expand Down
7 changes: 5 additions & 2 deletions server/models/schema.model.js
Expand Up @@ -25,7 +25,10 @@ const SchemaSchema = new mongoose.Schema({
versionKey: false
});

//pre save check the type, if type is platform - modes = true
// else model = false
SchemaSchema.path('category').validate(function (value) {
if (!value) return false;
return true;
}, 'Category is required');


module.exports = mongoose.model('Schema', SchemaSchema);
2 changes: 2 additions & 0 deletions server/routes/schema.route.js
Expand Up @@ -38,11 +38,13 @@ async function tree(req, res) {

async function insert(req, res) {
let schema = await schemaCtrl.insert(req.body);
if(!schema) throw new httpError(403);
res.json(schema);
}

async function upload(req, res) {
let schema = await schemaCtrl.upload(req);
if(!schema) throw new httpError(403);
res.json(schema);
}

Expand Down

0 comments on commit 1ed9da6

Please sign in to comment.