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

Incorrect error message returned on nested array rules #86

Open
0xMurage opened this issue Jan 8, 2020 · 4 comments
Open

Incorrect error message returned on nested array rules #86

0xMurage opened this issue Jan 8, 2020 · 4 comments

Comments

@0xMurage
Copy link

0xMurage commented Jan 8, 2020

Example

    const validator = new Validator({
        users: new Rule({
            type: "array",
            of: new Rule({type: "int", oneOf: [2, 3, 4, 5]}, "User ID does not exist")
        }, "Users' ID not provided"),
    });


validator.getErrors([2,4,7,10])  // returns Users' ID not provided

@oussamahamdaoui
Copy link
Owner

oussamahamdaoui commented Jan 14, 2020

Hey, sorry for the late response, i think that you made an error using the validator, the test should be something like this:

const {Validator,Rule } = require("@cesium133/forgjs");

const UserRule = new Rule({
    type:"array", 
    of: new Rule({type:"int", oneOf:[1, 2, 3]})
}, "User ID does not exist");

const validator = new Validator({
        users: UserRule,
    });

validator.getErrors({users: [1, 5]}); 

@0xMurage
Copy link
Author

Hey, if the array rule object on the of key accepts a validator or a new rule instance, I would have expected each rule to also accept an error message parameter. This behaviour can be useful when validating nested array objects e.g.

 {
    users: [{
        role: {
            name: "admin",
            permissions: [
              {id: 12, name: "add_user"}, 
              { id: 4,name: "delete_user"}
            ]
        }
    }]
}

@oussamahamdaoui
Copy link
Owner

Sorry I misunderstood your problem, can you give me an example of both the error and the expected result, in the firs example you are testing it against an array witch doesn’t have a key ‘users’

@0xMurage
Copy link
Author

Thank you for prompt reply. Here is how to reproduce the issue

const {Validator, Rule} = require("@cesium133/forgjs");

const validator = new Validator({
    users: new Rule({
        type: "array",
        of: new Validator({
            role: new Rule({type: "string"}, "user role is not valid"),
            user_id: new Rule({type: "number"}, "User ID is not valid")
        })
    }, "No user object provided"),
});

console.log(validator.getErrors({users: [{role: 1, user_id: 2}]})); //returns 'No user object provided'

The expected behaviour is to return "user role is not valid". The top most error message is returned when doing a nested validation of an object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants