adonisjs / validator

Schema based validator for AdonisJS
MIT License
115 stars 40 forks source link

Validate Array as Schema #164

Closed odparraj closed 1 year ago

odparraj commented 1 year ago

Why this feature is required (specific use-cases will be appreciated)?

We don't always want to validate a flat object as root, imagine the case where instead of an object it receives an array as input

If I had to explain with a Laravel example, it would be this:

use Illuminate\Support\Facades\Validator;

$input = [
    [
        'name' => 'Jhon Doe',
        'email' => 'jhon@test.com',
    ],
    [
        'name' => 'Dani Michelle',
        'email' => 'dani@test.com',
    ],
];

Validator::validate($input, [
    '*.name' => 'required|string',
    '*.email' => 'required|email',
]);

what is totally valid

imagine to validate the use case like this

validator.validate(schema, [
        {
            "name": "Jhon Doe",
            "email": "jhon@test.com"
        },
        {
            "name": "Dani Michelle",
            "email": "dani@test.com"
        }
]);

I don't know how I should create the schema

Have you tried any other work arounds?

I have reviewed the documentation but the only thing that occurs to me is to iterate and validate each item, it seems like a feature to develop

Another alternative would be to create an object that contains an key where it would send the array what you want to validate, but the output errors would be linked to that key.

Are you willing to work on it with little guidance?

yes of course

thetutlage commented 1 year ago

I am not sure why you decided to share a PHP example on a JavaScript framework repo. Imagine, someone one day comes with a Java or a Rust example?

The limitation is intentional. What do you call this top-level array? An array of contacts? If yes, then it should be like this.

{
  "contacts": [
    {
      "name": "Jhon Doe",
      "email": "jhon@test.com"
    },
    {
      "name": "Dani Michelle",
      "email": "dani@test.com"
    }
  ]
}
odparraj commented 1 year ago

the example in laravel is because I understand that adonis is inspired by this framework, just for that, I thought it would be familiar, however the proposed solution is not entirely correct, in fact I mentioned it in the alternatives, it would be good to read until the end, without However I think that schema.create(arrayOrJson) should be able to be defined as array or object after all they are valid inputs in json format

odparraj commented 1 year ago

imagine an http request body as an array and not as an object, this usually happens for bulk type apis, sometimes it is not possible to change the apis contract and the validations would expect a response of the same format of a schema.array but from the root, not tied to a contacts key

odparraj commented 1 year ago

now if the answer is: validate with the object and transform the validation messages by removing the reference to contact, this would be a workaround, not a solution for especific use case, thanks anyway