Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Joi validation in a Mongoose model

// validateMiddleware
module.exports = (validator) => {
  return (req, res, next) => {
    const { error } = validator(req.body)
    console.log('error: ', error)
    if (error) {
      return res.status(400).send(error.details[0].message)
    }
    next()
  }
}
Comment

mongoose model and joi validation

const mongoose = require("mongoose");
const Joi = require("joi");
Joi.objectId = require("joi-objectid")(Joi);

const { Schema } = mongoose;

const postSchema = new Schema(
  {
    userId: {
      type: mongoose.Schema.ObjectId,
      ref: "user",
    },
    name: {
      type: String,
    },
    description: {
      type: String,
    },
    recipeStepValue: {
      type: Array,
    },
    setpsCount: {
      type: String,
    },
    postType: {
      type: String,
    },
    image: [
      {
        type: String,
      },
    ],
    location: {
      fullAddress: {
        type: String,
      },
      address: {
        type: String,
      },
      postAddress: {
        type: String,
      },
      city: {
        type: String,
      },
      state: {
        type: String,
      },
      country: {
        type: String,
      },
      postcode: {
        type: String,
      },
      longitude: {
        type: Number,
      },
      latitude: {
        type: Number,
      },
    },
    video: [
      {
        type: String,
      },
    ],
    mediaFiles: [{ type: String }],
    tags: {
      type: Array,
    },
    difficulty: {
      type: String,
    },
    comments: {
      type: mongoose.Schema.ObjectId,
      ref: "comment",
    },
    likes: { type: mongoose.Schema.ObjectId, ref: "likes" },
    ingredient: [
      {
        fdcId: String,
        label: String,
      },
    ],
    ingredients: [
      {
        id: {
          type: mongoose.Schema.ObjectId,
          ref: "ingredients",
        },
        quantity: { type: String },
        unit: { type: String },
      },
    ],
  },

  {
    timestamps: true,
  }
);
const Post = mongoose.model("post", postSchema);
const validatePost = (post) => {
  const schema = {
    userId: Joi.objectId().required(),
    description: Joi.any(),
    imageFile: Joi.any(),
    name: Joi.any(),
    location: Joi.any(),
    difficulty: Joi.string(),
    postType: Joi.string(),
    ingredient: Joi.any(),
    tags: Joi.any(),
  };
  return Joi.validate(post, schema);
};

module.exports.Post = Post;
module.exports.validatePost = validatePost;
Comment

PREVIOUS NEXT
Code Example
Javascript :: is js dead 
Javascript :: express fingerprint 
Javascript :: delete JSON properties in place with jq 
Javascript :: JSON to Ruby Hash Parser 
Javascript :: javascript add nd to number 
Javascript :: var s= 
Javascript :: display form input on console jquery 
Javascript :: json 
Javascript :: denuncia perturbação 
Javascript :: last underscore 
Python :: python generate folder if it not exist 
Python :: jupyter display all columns 
Python :: number table python 
Python :: plt figsize 
Python :: sort dataframe by column 
Python :: check python 32 or 64 
Python :: python clamp 
Python :: python print time 
Python :: matplotlib equal axis 
Python :: sorting by column in pandas 
Python :: how to change pygame window icon 
Python :: python download image 
Python :: set recursion limit python 
Python :: python delete directory if exists 
Python :: python loop through all folders and subfolders 
Python :: pandas replace column name spaces with underscore 
Python :: django model specify table name 
Python :: shapely polygon from string 
Python :: export data csv python 
Python :: split string into array every n characters python 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =