Skip to content

Rules in a a ResourceRequest are not checked against the Schema's defined fields; no exception thrown for an incorrect Request #278

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

Closed
Azeirah opened this issue May 7, 2024 · 2 comments

Comments

@Azeirah
Copy link

Azeirah commented May 7, 2024

I'm still new to Laravel JSON API. I had a

<?

class UpsellRequest extends ResourceRequest
{
  /**
   * Get the validation rules for the resource.
   *
   * @return array
   */
  public function rules(): array
  {
    return [
      "salesarea" => JsonApiRule::toOne(),
      "upsell_variant" => ["required", "string"],
      "name" => ["required", "string"],
    ];
  }
}

The accompanying schema is as follows

<?

class UpsellSchema extends Schema {
  /* ... */
  
   public function fields(): array
    {
      return [
        ID::make(),
        Str::make("name"),
        Str::make("upsell_variant"),
        BelongsTo::make("salesarea"),
        HasMany::make("upsell-products"),
        DateTime::make("createdAt")
          ->sortable()
          ->readOnly(),
        DateTime::make("updatedAt")
          ->sortable()
          ->readOnly(),
      ];
    }
}

I tried creating a new Upsell with a salesarea relation, and sent the following data

{
    "data": {
        "type": "upsells",
        "attributes": {
            "name": "Test",
            "upsell_variant": "upsell"
        },
        "relationships": {
            "salesarea": {
                "data": {
                    "type": "salesareas",
                    "id": "1"
                }
            }
        }
    }
}

The resource is created succesfully (200 OK), the Upsell model with name Test is created, all looks ok!

After inspecting the created model, I found that the salesarea_id is set to 0.

After a lot of searching in

  • The upsell schema
  • The salesarea schema
  • The relationships in the route
  • The upsell and salesarea policies
  • The data I'm sending on the frontend

I figured out that the issue lied with the Request, I had named rule salesareas (referring to the resource name: "salesareas") and not salesarea (referring to the field in the schema).

I'm noticing that in general it's very difficult to know which naming convention to use when, and I really hope it could be included in the base library to throw errors as early as possible to prevent having to search through all the related logic for a tiny singular typo or confusion between a schema field and relation.

@Azeirah Azeirah changed the title Rules in a a ResourceRequest are not checked against the defined fields Rules in a a ResourceRequest are not checked against the Schema's defined fields -- no exception thrown for an incorrect Request May 7, 2024
@Azeirah Azeirah changed the title Rules in a a ResourceRequest are not checked against the Schema's defined fields -- no exception thrown for an incorrect Request Rules in a a ResourceRequest are not checked against the Schema's defined fields; no exception thrown for an incorrect Request May 7, 2024
@lindyhopchris
Copy link
Contributor

Yeah agree this is tricky with the current version. You have to add validation rules, as we only use validated data. It's a bit of a gotcha that's caught people out.

I'm actually going to close this issue, as this is solved in the next major version. We're moving validation to the fields on the schema, which makes it clearer as to what's going on. You can see an example of that here:
#39 (comment)

@Azeirah
Copy link
Author

Azeirah commented May 8, 2024

Yeah agree this is tricky with the current version. You have to add validation rules, as we only use validated data. It's a bit of a gotcha that's caught people out.

I'm actually going to close this issue, as this is solved in the next major version. We're moving validation to the fields on the schema, which makes it clearer as to what's going on. You can see an example of that here: #39 (comment)

Ah, awesome! I'll keep a close eye on development, and will be providing feedback when I have some.

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