Skip to content

fix(json-schema): Validate document's links according to the JSONAPI specification #1487

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

Conversation

guillemcanal
Copy link

@guillemcanal guillemcanal commented Jun 30, 2020

Hi,

I believe that the current JSON Schema do not reflect the written specification regarding document's links and pagination links.

  1. It seems that the allOf keyword is misused in #/definitions/success/properties/links.

According to the JSON Schema spec:

An instance validates successfully against this keyword if it validates successfully against all schemas defined by this keyword's value. (see in context)

  1. Pagination links (next, prev, last, first) MAY be either a #/definitions/link or null

I tested the fix against various examples provided on the JSONAPI website such as:

A resource with links:

{
    "data": {
        "type": "articles",
        "id": "1",
        "attributes": {
            "title": "Rails is Omakase"
        },
        "relationships": {
            "author": {
                "links": {
                    "self": "/articles/1/relationships/author",
                    "related": "/articles/1/author"
                },
                "data": {
                    "type": "people",
                    "id": "9"
                }
            }
        }
    }
}

A collection with pagination links as string

{
  "meta": {
    "totalPages": 13
  },
  "data": [
    {
      "type": "articles",
      "id": "3",
      "attributes": {
        "title": "JSON:API paints my bikeshed!",
        "body": "The shortest article. Ever.",
        "created": "2015-05-22T14:56:29.000Z",
        "updated": "2015-05-22T14:56:28.000Z"
      }
    }
  ],
  "links": {
    "self": "http://example.com/articles?page%5Bnumber%5D=3&page%5Bsize%5D=1",
    "first": "http://example.com/articles?page%5Bnumber%5D=1&page%5Bsize%5D=1",
    "prev": "http://example.com/articles?page%5Bnumber%5D=2&page%5Bsize%5D=1",
    "next": "http://example.com/articles?page%5Bnumber%5D=4&page%5Bsize%5D=1",
    "last": "http://example.com/articles?page%5Bnumber%5D=13&page%5Bsize%5D=1"
  }
}

A collection with pagination links as link objects:

{
  "meta": {
    "totalPages": 13
  },
  "data": [
    {
      "type": "articles",
      "id": "3",
      "attributes": {
        "title": "JSON:API paints my bikeshed!",
        "body": "The shortest article. Ever.",
        "created": "2015-05-22T14:56:29.000Z",
        "updated": "2015-05-22T14:56:28.000Z"
      }
    }
  ],
  "links": {
    "self": { "href": "http://example.com/articles?page%5Bnumber%5D=3&page%5Bsize%5D=1"},
    "first": { "href": "http://example.com/articles?page%5Bnumber%5D=1&page%5Bsize%5D=1"},
    "prev": { "href": "http://example.com/articles?page%5Bnumber%5D=2&page%5Bsize%5D=1"},
    "next": { "href": "http://example.com/articles?page%5Bnumber%5D=4&page%5Bsize%5D=1"},
    "last": { "href": "http://example.com/articles?page%5Bnumber%5D=13&page%5Bsize%5D=1"}
  }
}

A collection with a null pagination links:

{
  "meta": {
    "totalPages": 13
  },
  "data": [
    {
      "type": "articles",
      "id": "3",
      "attributes": {
        "title": "JSON:API paints my bikeshed!",
        "body": "The shortest article. Ever.",
        "created": "2015-05-22T14:56:29.000Z",
        "updated": "2015-05-22T14:56:28.000Z"
      }
    }
  ],
  "links": {
    "first": null,
    "prev": null,
    "next":null,
    "last": null
  }
}

A collection with mixed links (standard links and pagination links)

{
  "meta": {
    "totalPages": 13
  },
  "data": [
    {
      "type": "articles",
      "id": "3",
      "attributes": {
        "title": "JSON:API paints my bikeshed!",
        "body": "The shortest article. Ever.",
        "created": "2015-05-22T14:56:29.000Z",
        "updated": "2015-05-22T14:56:28.000Z"
      }
    }
  ],
  "links": {
    "foo": {
        "href": "https://foo.com/bar",
        "mata": {
            "bar": "baz"
        }
    },
    "self": "http://example.com/articles?page%5Bnumber%5D=13&page%5Bsize%5D=1",
    "first": "http://example.com/articles?page%5Bnumber%5D=1&page%5Bsize%5D=1",
    "prev": "http://example.com/articles?page%5Bnumber%5D=12&page%5Bsize%5D=1",
    "next": null,
    "last": "http://example.com/articles?page%5Bnumber%5D=13&page%5Bsize%5D=1"
  }
}

@guillemcanal guillemcanal force-pushed the fix/json-schema-links-members branch from 7ee016b to c899c75 Compare June 30, 2020 01:00
@guillemcanal guillemcanal changed the title fix(json-schema): Accurately validate links members related to the primary data fix(json-schema): Validate document's links according to the JSONAPI specification Jun 30, 2020
@guillemcanal guillemcanal force-pushed the fix/json-schema-links-members branch from c899c75 to 0ebec29 Compare June 30, 2020 01:19
@dgeb
Copy link
Member

dgeb commented Feb 25, 2021

@guillemcanal thanks for working on this! I reviewed this with @gabesullice and we both agree it looks to be an improvement.

@Relequestual as our "resident" JSON Schema expert, would you mind reviewing?

@Relequestual
Copy link

@dgeb I'd be happy to. Mentioned here: #1522 (comment)

I'm building a compliance suite and it's taken a little longer than expected, but I'll get back to fixing the schema for 1.0 soon, likely Monday.
If you see nothing say in a weeks time, please do nag me on Twitter. Same username there as here =]

@guillemcanal
Copy link
Author

guillemcanal commented Mar 31, 2021

Closing in favor of #1555

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

Successfully merging this pull request may close these issues.

6 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy