JSON:API compliance - optional attributes

Aperta
#197 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
typescript
Ambito
api

Direzione di ricerca

Inizia individuando le interfacce SingleResourceDoc, CollectionResourceDoc e AttributesObject e verificando i relativi vincoli generici e le firme di indice. Riproduci l’esempio di proprietà opzionale dell’issue con il compilatore TypeScript, quindi verifica che gli attributi opzionali siano accettati senza indebolire le restrizioni JSON:API dichiarate.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
TypeScript
Stelle
54
Fork
6
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di mike-north/jsonapi-typescript

Tutte le issue di mike-north/jsonapi-typescript

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.