Skip to content

Complex attribute values validation? #18

Closed
@nickelozz

Description

@nickelozz

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions