Joi validation check if record exists in database
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 22 hours ago
mhndev
6321024
6321024
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
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
20 hours ago
add a comment |
up vote
0
down vote
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 |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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
20 hours ago
add a comment |
up vote
1
down vote
accepted
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
20 hours ago
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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 21 hours ago
Senthil
6981515
6981515
thanks, It solved my issue.
– mhndev
20 hours ago
add a comment |
thanks, It solved my issue.
– mhndev
20 hours ago
thanks, It solved my issue.
– mhndev
20 hours ago
thanks, It solved my issue.
– mhndev
20 hours ago
add a comment |
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
up vote
0
down vote
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 21 hours ago
Sanket
333213
333213
add a comment |
add a comment |
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