Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get relative path instead of absolute when validating child associated models #9524

Open
serge1peshcoff opened this issue Jun 11, 2018 · 2 comments
Labels
type: feature For issues and PRs. For new features. Never breaking changes.

Comments

@serge1peshcoff
Copy link

What are you doing?

I have this schema structure:

const Event = sequelize.define('event', {
    // fields defined
});

const Question = sequelize.define('question', {
    description: {
        type: Sequelize.STRING,
        allowNull: false,
        defaultValue: '',
        validate: {
            notEmpty: { msg: 'Description should be set.' }
       },
    },
    // other fields defined
});

Event.hasMany(Question);
Question.belongsTo(Event);

Then I'm creating an Event instance with Question instance this way:

const body = {
    questions: [
        { description: '' } // is obviously invalid
    ],
    // some other fields
}

const newEvent = await Event.create(body, {
    include: [ Question ]
});

What is happening?

I am getting SequelizeValidationError (which is what's expected) with one ValidationErrorItem (also expected), but the path value of it is absolute ('description'):

      {
        "message": "Description should be set.",
        "type": "Validation error",
        "path": "description",
        "value": "",
        "origin": "FUNCTION",
        "instance": {
          "required": true,
          "id": null,
          "description": "",
          "event_id": 60,
          "updated_at": "2018-06-11T12:25:04.666Z",
          "created_at": "2018-06-11T12:25:04.666Z"
        },
        "validatorKey": "notEmpty",
        "validatorName": "notEmpty",
        "validatorArgs": [
          {
            "msg": "Description should be set."
          }
        ],
        "__raw": {
          "validatorName": "notEmpty",
          "validatorArgs": [
            {
              "msg": "Description should be set."
            }
          ]
        }
      }

What do you expect to happen?

It would be awesome to get the relative path value, like in Mongoose (if I'd do simular scheme in Mongoose, the path value would be something like questions.0.description. That way it would be more clear which child is invalid (especially with multiple children, or if your parent model has fields which have the same name as the children model).

Dialect: postgres
Dialect version:

$ npm ls | grep pg
├─┬ pg@7.4.3
│ ├── pg-connection-string@0.1.3
│ ├── pg-pool@2.0.3
│ ├─┬ pg-types@1.12.1
│ ├─┬ pgpass@1.0.2

Database version: PostgreSQL 9.6
Sequelize version:

$ npm ls | grep sequelize
├─┬ sequelize@4.37.10
├─┬ sequelize-cli@4.0.0

Tested with latest release: No

@sushantdhiman sushantdhiman added the type: feature For issues and PRs. For new features. Never breaking changes. label Jun 12, 2018
@stale
Copy link

stale bot commented Jul 24, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

@stale stale bot added the stale label Jul 24, 2019
@papb papb removed the stale label Jul 24, 2019
@davidrac
Copy link

davidrac commented May 8, 2024

This feature will make it possible to return errors with enough information to present in the front end on the corresponding input fields. Without it, it's quite difficult.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature For issues and PRs. For new features. Never breaking changes.
Projects
None yet
Development

No branches or pull requests

4 participants