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 ?










share|improve this question


























    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 ?










    share|improve this question
























      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 ?










      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 22 hours ago









      mhndev

      6321024




      6321024
























          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






          share|improve this answer





















          • thanks, It solved my issue.
            – mhndev
            20 hours ago


















          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.






          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',
            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








            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






            share|improve this answer





















            • thanks, It solved my issue.
              – mhndev
              20 hours ago















            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






            share|improve this answer





















            • thanks, It solved my issue.
              – mhndev
              20 hours ago













            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






            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 21 hours ago









            Senthil

            6981515




            6981515












            • 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




            thanks, It solved my issue.
            – mhndev
            20 hours ago












            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.






            share|improve this answer

























              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.






              share|improve this answer























                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.






                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 21 hours ago









                Sanket

                333213




                333213






























                     

                    draft saved


                    draft discarded



















































                     


                    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







                    Popular posts from this blog

                    Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                    Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                    A Topological Invariant for $pi_3(U(n))$