
Overview
This is a note about using the following repository to validate JSON:API compliance.
https://github.com/elliotttf/jsonapi-validator
At the time of writing this article, it appears to have not been updated for 7 years, so it may not support the latest schemas, but I was able to perform simple validation.
Usage
I prepared the following repository to try the library above.
https://github.com/nakamura196/jsonapi-validator-demo
Installation
This assumes the use of nvm, but it is not required.
git clone https://github.com/nakamura196/jsonapi-validator-demo
cd jsonapi-validator-demo
nvm i 22
nvm use 22
pnpm i
Testing
Valid Example
{
"jsonapi": {
"version": "1.0",
"meta": {
"links": {
"self": {
"href": "http://jsonapi.org/format/1.0/"
}
}
}
},
"data": [
{
"type": "record",
"id": "10_A0024853",
"attributes": {
"title": "サンプル"
}
}
]
}
./node_modules/jsonapi-validator/bin/jsonapi-validator.js -f ./01_valid.json
01_valid.json is valid JSON API.
Invalid Example: Unnecessary Properties
There is an unnecessary property aaa.
{
"jsonapi": {
"version": "1.0",
"meta": {
"links": {
"self": {
"href": "http://jsonapi.org/format/1.0/"
}
}
}
},
"aaa": {
"bbb": "ccc"
},
"data": [
{
"type": "record",
"id": "10_A0024853",
"attributes": {
"title": "サンプル"
}
}
]
}
./node_modules/jsonapi-validator/bin/jsonapi-validator.js -f ./02_invalid_additional_properties.json
Invalid JSON API.
should NOT have additional properties.
schemaPath: #/additionalProperties
additionalProperty: aaa
should NOT have additional properties.
schemaPath: #/additionalProperties
additionalProperty: aaa
should NOT have additional properties.
schemaPath: #/additionalProperties
additionalProperty: data
should have required property 'errors'.
schemaPath: #/required
missingProperty: errors
should NOT have additional properties.
schemaPath: #/additionalProperties
additionalProperty: aaa
should NOT have additional properties.
schemaPath: #/additionalProperties
additionalProperty: data
should have required property 'meta'.
schemaPath: #/required
missingProperty: meta
should match exactly one schema in oneOf.
schemaPath: #/oneOf
Invalid Example: Missing Required Properties
This is an example where the required property type is missing.
{
"jsonapi": {
"version": "1.0",
"meta": {
"links": {
"self": {
"href": "http://jsonapi.org/format/1.0/"
}
}
}
},
"data": [
{
"id": "10_A0024853",
"attributes": {
"title": "サンプル"
}
}
]
}
./node_modules/jsonapi-validator/bin/jsonapi-validator.js -f ./03_invalid_missing_property.json
Invalid JSON API.
should be object.
schemaPath: #/type
type: object
should have required property 'type'.
schemaPath: #/required
missingProperty: type
should be null.
schemaPath: #/oneOf/2/type
type: null
should match exactly one schema in oneOf.
schemaPath: #/oneOf
should NOT have additional properties.
schemaPath: #/additionalProperties
additionalProperty: data
should have required property 'errors'.
schemaPath: #/required
missingProperty: errors
should NOT have additional properties.
schemaPath: #/additionalProperties
additionalProperty: data
should have required property 'meta'.
schemaPath: #/required
missingProperty: meta
should match exactly one schema in oneOf.
schemaPath: #/oneOf
Summary
I hope this is helpful for using JSON:API.