cloudcreativity / json-api

Framework agnostic JSON API serialisation and deserialisation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Complex attribute values validation?

nickelozz opened this issue · comments

Hello!

While using getResourceObject from the JsonApiController I haven't been successful to validate JSON object attribute values whereas with Arrays ones it's working without any issue. I'll show you a couple of examples.

Array value validation:

// Validation rules
protected $rules = [
    "numbers" => ["array"],
    "numbers.0" => ["integer"],
    "numbers.1" => ["integer"],
];

// Request content
{
    "data": {
        "type": "users",
        "attributes": {
            "numbers": ["123456789","987654321"]
        }
    }
}

// Everything works like a charm

JSON object value validation:

// Validation rules
protected $rules = [
    //"objects" => ["array"],
    "objects.attr1" => ["integer"],
    "objects.attr2" => ["integer"],
];

// Request content
{
    "data": {
        "type": "users",
        "attributes": {
            "objects": {
                "attr1": "123456789",
                "attr2": "987654321"
            }
        }
    }
}

// Makes me a sad panda

In fact, in the second example if I uncomment this line: //"objects" => ["array"], I'll get an error on the objects attribute value as it's not an array and I'm not able to access the attr1 and attr2 with dot notation because of the same reason.

Is there a specific way to declare the rules to validate JSON objects attribute values or is there any other way to go about this? maybe consider transforming every object position inside "attributes" into arrays so the default dot notation validation way works?

@nickelozz

This is a bug - the validator should be recursively changing the attributes object to an array before validating it. I'll fix it and get back to you!

Closing here as this is an issue for the cloudcreativity/laravel-json-api package.

@nickelozz this is fixed - it's tagged as v0.2.1 of laravel-json-api