GithubHelp home page GithubHelp logo

diegohaz / bodymen Goto Github PK

View Code? Open in Web Editor NEW
48.0 4.0 14.0 625 KB

Body parser middleware for MongoDB, Express and Nodejs (MEN)

License: Other

JavaScript 100.00%
body-parser request-body mongodb mongoose nodejs express express-middleware

bodymen's Issues

Is body-parser really needed?

You mention in readme that body-parser is needed, but you dont use it in your rest-generator project, how is that? Thanks!

Error with nested properties

Hi, I'm trying to use bodymen with nested properties like this:

{
  'Name': 'Marc',
  'Address' : {
          'Street' : 'My Street',
          'City' : 'My City'      
   }
}

But I have an error in the middleware:

ValidationError: Address: Cast to Object failed for value "[object Object]" at path "Address"

Any ideas? Thanks.

Issue with required field type

Reproduction:
Using a basic express prototype:

router.post(
  '/',
  bodymen.middleware({
    fieldA: { type: String, required: true },
    fieldB: { type: String, required: "randomstring" },
    fieldC: { type: String, required: () => { return false } }
  }),
  (req, res, next) => {
     res.end("done")
  }
)   

All the 3 fields fieldA .. C becomes a required = true case. Calling it will return that all fields are required. I was hoping that atleast 'fieldC' should work as expected.
Did I miss anything?

Trying to get in touch regarding a security issue

Hey there!

I'd like to report a security issue but cannot find contact instructions on your repository.

If not a hassle, might you kindly add a SECURITY.md file with an email, or another contact method? GitHub recommends this best practice to ensure security issues are responsibly disclosed, and it would serve as a simple instruction for security researchers in the future.

Thank you for your consideration, and I look forward to hearing from you!

(cc @huntr-helper)

Mongoose, bodymen and pre validate a collection ref

Hi there! A have a question about mongoose and pre validate.

Is it possible to pre validate the request using Mongoose and bodymen and schema.tree with reference other collection as a field?
.
I need to validate the "user" and "location" fields.

import { Router } from 'express';
import { middleware as body } from 'bodymen';

import { token } from '../../services/passport';

import { create } from './controller';
import Company, { schema } from './model';

const router = new Router();
const {
  user,
  social_name,
...
  location,
...
} = schema.tree;

router.post('/',
  token({ required: true }),
  body({
    user,
...
   location,
...
}),
  create);

export default router;

The company model:

import mongoose, { Schema } from 'mongoose';
import mongooseKeywords from 'mongoose-keywords';


const statusList = ['a', 'd'];

const CompanySchema = new Schema({
  user: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true,
  },
  social_name: {
    type: String,
    trim: true,
    default: null,
  },
  fantasy_name: {
    type: String,
    trim: true,
...
  location: {
    type: Schema.Types.ObjectId,
    ref: 'Location',
    required: true,
  },
...
}

In the location field, if I POST a JSON object, it process like that:

{
  "formatted_address": "RN143, Mendoza, Argentina",
  "id": "5ba416cb667...",
  "name": "Ruta Nacional 143",
  "place_id": "C_EhlSTjE0MywgTWVuZ..."
}

And shows me an error as below:

ValidationError: location: Cast to ObjectID failed for value "[object Object]" at path "location", user: Path `user` is required.

Array of objects sending '[object Object]' to controller

I have the following field being send to node app (created off your generator btw)

links: [{'icon':'path to icon'}]

but once it gets to the controller, it fails due to its converted to '[object Object]'. Confirmed that the array of object is correct if I remove the

body({ links })

Suggestions or bug? Thanks

How to make validations?

I want to create validation for phoneNumber
phoneNumber:{ type: String, validate: /\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/ }
or to choose validate that value equals only one of provided
productType: { type: String, validate: ['bread', 'milk'] }

A required field should be ignored in case of update

Most of the time the Required statement is only applicable for creating the documents. After creation I cannot force the user to fill these fields again. Is there any option to bypass this?

I am copying the schema tree

const schemaTree = schema.tree
delete schemaTree._id;
delete schemaTree.updatedAt;
delete schemaTree.createdAt;
delete schemaTree.__v;

router.put('/:id',
  body( schemaTree ),
  update)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.