Joi validation check if record exists in database
I'm using Joi validation for validating input data,
consider I'm going to create a service and post body is :
{"name": "service A", "active": 1, "provider_id": 2}
I need to be able to check if in mongodb providers collection, record with id: 2
exists.
is Joi database aware at all ? or should I do sth else ?
node.js validation express joi
add a comment |
I'm using Joi validation for validating input data,
consider I'm going to create a service and post body is :
{"name": "service A", "active": 1, "provider_id": 2}
I need to be able to check if in mongodb providers collection, record with id: 2
exists.
is Joi database aware at all ? or should I do sth else ?
node.js validation express joi
add a comment |
I'm using Joi validation for validating input data,
consider I'm going to create a service and post body is :
{"name": "service A", "active": 1, "provider_id": 2}
I need to be able to check if in mongodb providers collection, record with id: 2
exists.
is Joi database aware at all ? or should I do sth else ?
node.js validation express joi
I'm using Joi validation for validating input data,
consider I'm going to create a service and post body is :
{"name": "service A", "active": 1, "provider_id": 2}
I need to be able to check if in mongodb providers collection, record with id: 2
exists.
is Joi database aware at all ? or should I do sth else ?
node.js validation express joi
node.js validation express joi
asked Nov 19 '18 at 9:55
mhndevmhndev
6431125
6431125
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
For MongoDB, mongoose can be used together with joigoose, which easily converts a Joi schema. Because joi is not a ORM
For ex unique email validation
// rating.js
const mongoose = require('mongoose')
const joigoose = require('joigoose')(mongoose)
// Require the 'ratings' schema
const schema = require('./rating-schema')
// Convert joi to mongoose schema
const mongooseSchema = joigoose.convert(schema);
// Modify some fields with database specific instructions
mongooseSchema.email.unique = true
// Add fields which don't make sense on the schema validator
mongooseSchema.updatedAt = { type: Date, default: Date.now }
mongooseSchema.createdAt = { type: Date, default: Date.now }
// Define mongoose model
const Ratings = mongoose.model('Ratings', mongooseSchema)
module.exports = Ratings
Ref: https://codeburst.io/joi-validate-input-and-define-databases-in-javascript-84adc6f1474b
Ref: https://github.com/yoitsro/joigoose
thanks, It solved my issue.
– mhndev
Nov 19 '18 at 11:50
add a comment |
joi is simply a JSON validator tool, there is no provision of database connectivity in it.
You can use direct DB query or ORM to check whether record present or not.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372111%2fjoi-validation-check-if-record-exists-in-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
For MongoDB, mongoose can be used together with joigoose, which easily converts a Joi schema. Because joi is not a ORM
For ex unique email validation
// rating.js
const mongoose = require('mongoose')
const joigoose = require('joigoose')(mongoose)
// Require the 'ratings' schema
const schema = require('./rating-schema')
// Convert joi to mongoose schema
const mongooseSchema = joigoose.convert(schema);
// Modify some fields with database specific instructions
mongooseSchema.email.unique = true
// Add fields which don't make sense on the schema validator
mongooseSchema.updatedAt = { type: Date, default: Date.now }
mongooseSchema.createdAt = { type: Date, default: Date.now }
// Define mongoose model
const Ratings = mongoose.model('Ratings', mongooseSchema)
module.exports = Ratings
Ref: https://codeburst.io/joi-validate-input-and-define-databases-in-javascript-84adc6f1474b
Ref: https://github.com/yoitsro/joigoose
thanks, It solved my issue.
– mhndev
Nov 19 '18 at 11:50
add a comment |
For MongoDB, mongoose can be used together with joigoose, which easily converts a Joi schema. Because joi is not a ORM
For ex unique email validation
// rating.js
const mongoose = require('mongoose')
const joigoose = require('joigoose')(mongoose)
// Require the 'ratings' schema
const schema = require('./rating-schema')
// Convert joi to mongoose schema
const mongooseSchema = joigoose.convert(schema);
// Modify some fields with database specific instructions
mongooseSchema.email.unique = true
// Add fields which don't make sense on the schema validator
mongooseSchema.updatedAt = { type: Date, default: Date.now }
mongooseSchema.createdAt = { type: Date, default: Date.now }
// Define mongoose model
const Ratings = mongoose.model('Ratings', mongooseSchema)
module.exports = Ratings
Ref: https://codeburst.io/joi-validate-input-and-define-databases-in-javascript-84adc6f1474b
Ref: https://github.com/yoitsro/joigoose
thanks, It solved my issue.
– mhndev
Nov 19 '18 at 11:50
add a comment |
For MongoDB, mongoose can be used together with joigoose, which easily converts a Joi schema. Because joi is not a ORM
For ex unique email validation
// rating.js
const mongoose = require('mongoose')
const joigoose = require('joigoose')(mongoose)
// Require the 'ratings' schema
const schema = require('./rating-schema')
// Convert joi to mongoose schema
const mongooseSchema = joigoose.convert(schema);
// Modify some fields with database specific instructions
mongooseSchema.email.unique = true
// Add fields which don't make sense on the schema validator
mongooseSchema.updatedAt = { type: Date, default: Date.now }
mongooseSchema.createdAt = { type: Date, default: Date.now }
// Define mongoose model
const Ratings = mongoose.model('Ratings', mongooseSchema)
module.exports = Ratings
Ref: https://codeburst.io/joi-validate-input-and-define-databases-in-javascript-84adc6f1474b
Ref: https://github.com/yoitsro/joigoose
For MongoDB, mongoose can be used together with joigoose, which easily converts a Joi schema. Because joi is not a ORM
For ex unique email validation
// rating.js
const mongoose = require('mongoose')
const joigoose = require('joigoose')(mongoose)
// Require the 'ratings' schema
const schema = require('./rating-schema')
// Convert joi to mongoose schema
const mongooseSchema = joigoose.convert(schema);
// Modify some fields with database specific instructions
mongooseSchema.email.unique = true
// Add fields which don't make sense on the schema validator
mongooseSchema.updatedAt = { type: Date, default: Date.now }
mongooseSchema.createdAt = { type: Date, default: Date.now }
// Define mongoose model
const Ratings = mongoose.model('Ratings', mongooseSchema)
module.exports = Ratings
Ref: https://codeburst.io/joi-validate-input-and-define-databases-in-javascript-84adc6f1474b
Ref: https://github.com/yoitsro/joigoose
answered Nov 19 '18 at 10:51
SenthilSenthil
7921515
7921515
thanks, It solved my issue.
– mhndev
Nov 19 '18 at 11:50
add a comment |
thanks, It solved my issue.
– mhndev
Nov 19 '18 at 11:50
thanks, It solved my issue.
– mhndev
Nov 19 '18 at 11:50
thanks, It solved my issue.
– mhndev
Nov 19 '18 at 11:50
add a comment |
joi is simply a JSON validator tool, there is no provision of database connectivity in it.
You can use direct DB query or ORM to check whether record present or not.
add a comment |
joi is simply a JSON validator tool, there is no provision of database connectivity in it.
You can use direct DB query or ORM to check whether record present or not.
add a comment |
joi is simply a JSON validator tool, there is no provision of database connectivity in it.
You can use direct DB query or ORM to check whether record present or not.
joi is simply a JSON validator tool, there is no provision of database connectivity in it.
You can use direct DB query or ORM to check whether record present or not.
answered Nov 19 '18 at 10:30
SanketSanket
481415
481415
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372111%2fjoi-validation-check-if-record-exists-in-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown