limoncello-php / app

Quick start JSON API application

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ReadQuery implementing JsonApiQueryRulesInterface

dreamsbond opened this issue · comments

do you have some hints on enabling sparse fieldset?

    /**
     * @return RuleInterface[]|null
     */
    public static function getFieldSetRules(): ?array
    {
        // no field sets are allowed
//        return [];
        return [
            Schema::RESOURCE_ID     => r::stringToInt(r::moreThan(0)),
            Schema::ATTR_NAME       => r::asSanitizedString(),
            Schema::ATTR_FIRST_NAME => r::asSanitizedString(),
            Schema::ATTR_LAST_NAME  => r::asSanitizedString(),
            Schema::ATTR_NAME       => r::asSanitizedString(),
        ];
    }

does not show sparse fieldset by fields[users]=id,name

outputting:

{
    "errors": [
        {
            "status": "422",
            "title": "The value is invalid.",
            "detail": "The value is invalid.",
            "source": {
                "parameter": "fields"
            }
        }
    ]
}

return null on getFilterRules(), getFieldSetRules() does allow all field to be filterable and selective
but how to define specific fields?

Hi, sorry for late reply. I've been fully busy recent days.

It looks you're trying 0.9.x, right?

If it returns null it allows any inputs (no check), if it returns empty array [] it means nothing allowed, if it returns an array with ['field-name' => $rule] only listed fields are allowed (e.g. 'field-name').

Also you might be confusing JSON fields with fieldsets. The method you need is method getFilterRules but not getFieldSetRules

I've reproduced it locally and looking closer at it at the moment.

That's a bug. Sorry, that's on my side. I didn't pay enough attention to this feature.

Firstly as the spec says it is possible to have multiple fieldsets. Currently, it implies fieldset rules only for the main type.

So getFieldSetRules should look like

    public static function getFieldSetRules(): array
    {
        return [
            // if fields sets are given only the following fields are OK
            Schema::TYPE     => r::inValues([Schema::RESOURCE_ID, Schema::ATTR_FIRST_NAME, Schema::ATTR_LAST_NAME]),
            // roles field sets could be any
            RoleSchema::TYPE => r::success(),
        ];
    }

UPDATE I've modified the code above and it should work with current version. The fixed code will be included in the next application's release.

Oh. Yes. I misused the syntax filter query rules on
spare fieldset rule.

And thanks for the update of multiple sparse fieldset rules!