Joi validation check if record exists in database












0















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 ?










share|improve this question



























    0















    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 ?










    share|improve this question

























      0












      0








      0








      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 ?










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 9:55









      mhndevmhndev

      6431125




      6431125
























          2 Answers
          2






          active

          oldest

          votes


















          2














          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






          share|improve this answer
























          • thanks, It solved my issue.

            – mhndev
            Nov 19 '18 at 11:50



















          0














          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.






          share|improve this answer























            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            2














            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






            share|improve this answer
























            • thanks, It solved my issue.

              – mhndev
              Nov 19 '18 at 11:50
















            2














            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






            share|improve this answer
























            • thanks, It solved my issue.

              – mhndev
              Nov 19 '18 at 11:50














            2












            2








            2







            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






            share|improve this answer













            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







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 '18 at 10:51









            SenthilSenthil

            7921515




            7921515













            • 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





            thanks, It solved my issue.

            – mhndev
            Nov 19 '18 at 11:50













            0














            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.






            share|improve this answer




























              0














              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.






              share|improve this answer


























                0












                0








                0







                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.






                share|improve this answer













                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 19 '18 at 10:30









                SanketSanket

                481415




                481415






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini