Skip to content
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

JSON:API compliance - optional attributes #197

Open
MaSpeng opened this issue Jan 12, 2021 · 0 comments
Open

JSON:API compliance - optional attributes #197

MaSpeng opened this issue Jan 12, 2021 · 0 comments

Comments

@MaSpeng
Copy link

MaSpeng commented Jan 12, 2021

Is your feature request related to a problem? Please describe.
I would like to use the types in your library to describe the interfaces in our application to consume them in the backend and frontend libraries.

Currently, I have an issue with optional attributes that are not allowed by the index signature of the { [k: string]: JSON.Value } which is used by the SingleResourceDoc, CollectionResourceDoc and AttributesObject interfaces.

Following a simplified and JSON:API compliant use case:

Example response body 1:

{
  "data": {
    "id": "1",
    "type": "_resource_type",
    "attributes": {
        "mandatory_attribute": "mandatory value"
    }
  }
}

Example response body 2:

{
  "data": {
    "id": "2",
    "type": "_resource_type",
    "attributes": {
        "mandatory_attribute": "mandatory value",
        "optional_property": "optional value",
    }
  }
}

Expected interface specification:

export type ResourceRequestBody = SingleResourceDoc<
  '_resource_type_',
  {
    mandatory_property: string;
    optional_property?: string;
  }
>;

This scenario would lead to the following typescript error:

TS2344: Type '{ mandatory_property: string; optional_property?: string | undefined; }' does not satisfy the constraint '{ [k: string]: Value; }'.
   Property 'optional_property' is incompatible with index signature.
     Type 'string | undefined' is not assignable to type 'Value'.
       Type 'undefined' is not assignable to type 'Value'.

As described in the JSON:API specification (json-schema), attributes has to be an object which may contain properties with a naming scheme which matches the pattern ^[a-zA-Z0-9](?:[-\\w]*[a-zA-Z0-9])?$ and are not named relationships, links, id or type.

"attributes": {
      "description": "Members of the attributes object (\"attributes\") represent information about the resource object in which it's defined.",
      "type": "object",
      "patternProperties": {
        "^[a-zA-Z0-9](?:[-\\w]*[a-zA-Z0-9])?$": {
          "description": "Attributes may contain any valid JSON value."
        }
      },
      "not": {
        "anyOf": [
          {"required": ["relationships"]},
          {"required": ["links"]},
          {"required": ["id"]},
          {"required": ["type"]}
        ]
      },
      "additionalProperties": false
    },

Describe the solution you'd like
I would appreciate the support for optional attributes or an example of how to use them. Sadly I am not aware of how to change the interface signature to support optional attributes so I am not able to provide a PR at the moment.

Describe alternatives you've considered
I tried to pass different specifications to the generic to support optional attributes but the index signature always wins as expected.

Additional context

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

1 participant