Skip to content

Commit

Permalink
Completion of remaining JSON:API Errors.ErrorObjects Ruels and Test C…
Browse files Browse the repository at this point in the history
…ases

- Fully implemented and documented Spectral rules for JSON:API Errors.ErrorObjects as per JSON:API Specifications.
- Each rule checks specific aspects of the error objects, ensuring adherence to standards like array structure, member types, and URI formats.
- Added comprehensive test cases for all rules covering both positive and negative scenarios. This includes checks for array structures, object member validation, type enforcement, and specific member values.
- Enhanced documentation with detail jsdocs for key areas including helper functions, complex rule logic, JSONPath expressions, and individual test cases. This aims to improve code readability and maintainability.
- Included specific test documents for both valid and invalid API structures to validate each rule thoroughly.
- Conducted rigorous testing to ensure all rules and test cases function as expected, with a focus on reliability and accuracy in error object validation.

This commit represents a complete and robust solution for validation JSON:API error objects, ensuring compliance with the JSON:API specification and enhacning the overall quality of API error handling.
  • Loading branch information
ezenity committed Nov 30, 2023
1 parent 8fd5837 commit d8372b5
Show file tree
Hide file tree
Showing 15 changed files with 3,376 additions and 152 deletions.
163 changes: 74 additions & 89 deletions rules/jsonapi-errors-error-object.js

Large diffs are not rendered by default.

199 changes: 199 additions & 0 deletions test/docs/errors/errorObjects/invalidApiDocumentItemsCodeType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/* eslint-env mocha */
/* eslint-disable quotes */
const invalidApiDocumentItemsCodeType = {
"openapi": "3.1.0",
"info": {
"title": "User Information API",
"version": "1.0.0",
"description": "API for retrieving user information"
},
"paths": {
"/users/{userId}": {
"get": {
"tags": [
"users"
],
"summary": "Get User by ID",
"description": "Retrieves information for a specific user by their ID.",
"operationId": "getUserById",
"parameters": [
{
"name": "userId",
"in": "path",
"required": true,
"description": "Unique identifier of the user",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Successful response with user information",
"content": {
"application/vnd.api+json": {
"schema": {
"type": "object",
"properties": {
"data": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string",
"enum": [
"user"
]
},
"attributes": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"email": {
"type": "string"
}
// Other user attributes...
}
}
}
}
}
},
"examples": {
"user": {
"summary": "User Example",
"value": {
"data": {
"id": "12345",
"type": "user",
"attributes": {
"name": "John Doe",
"email": "[email protected]"
// Other user attributes...
}
}
}
}
}
}
}
},
"400": {
"description": "Bad Request",
"content": {
"application/vnd.api+json": {
"schema": {
"$ref": "#/components/schemas/JsonApiError"
}
}
}
},
"404": {
"description": "User Not Found",
"content": {
"application/vnd.api+json": {
"schema": {
"$ref": "#/components/schemas/JsonApiError"
}
}
}
},
"500": {
"description": "Internal Server Error",
"content": {
"application/vnd.api+json": {
"schema": {
"$ref": "#/components/schemas/JsonApiError"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Test": {
"type": "object",
"properties": {
"test": {
"type": "string"
}
}
},
"JsonApiError": {
"type": "object",
"properties": {
"errors": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ErrorObject"
}
}
}
},
"ErrorObject": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"links": {
"type": "object",
"properties": {
"about": {
"type": "string",
"format": "uri"
},
"type": {
"type": "string",
"format": "uri"
}
}
},
"status": {
"type": "string"
},
"code": {
// Original: "string"
"type": "object"
},
"title": {
"type": "string"
},
"detail": {
"type": "string"
},
"source": {
"type": "object",
"properties": {
"pointer": {
"type": "string"
},
"parameter": {
"type": "string"
},
"header": {
"type": "string"
}
}
},
"meta": {
"type": "object",
"additionalProperties": true
}
},
"required": [
"detail"
]
}
}
}
};

export default invalidApiDocumentItemsCodeType;
199 changes: 199 additions & 0 deletions test/docs/errors/errorObjects/invalidApiDocumentItemsDetailType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/* eslint-env mocha */
/* eslint-disable quotes */
const invalidApiDocumentItemsDetailType = {
"openapi": "3.1.0",
"info": {
"title": "User Information API",
"version": "1.0.0",
"description": "API for retrieving user information"
},
"paths": {
"/users/{userId}": {
"get": {
"tags": [
"users"
],
"summary": "Get User by ID",
"description": "Retrieves information for a specific user by their ID.",
"operationId": "getUserById",
"parameters": [
{
"name": "userId",
"in": "path",
"required": true,
"description": "Unique identifier of the user",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Successful response with user information",
"content": {
"application/vnd.api+json": {
"schema": {
"type": "object",
"properties": {
"data": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"type": {
"type": "string",
"enum": [
"user"
]
},
"attributes": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"email": {
"type": "string"
}
// Other user attributes...
}
}
}
}
}
},
"examples": {
"user": {
"summary": "User Example",
"value": {
"data": {
"id": "12345",
"type": "user",
"attributes": {
"name": "John Doe",
"email": "[email protected]"
// Other user attributes...
}
}
}
}
}
}
}
},
"400": {
"description": "Bad Request",
"content": {
"application/vnd.api+json": {
"schema": {
"$ref": "#/components/schemas/JsonApiError"
}
}
}
},
"404": {
"description": "User Not Found",
"content": {
"application/vnd.api+json": {
"schema": {
"$ref": "#/components/schemas/JsonApiError"
}
}
}
},
"500": {
"description": "Internal Server Error",
"content": {
"application/vnd.api+json": {
"schema": {
"$ref": "#/components/schemas/JsonApiError"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Test": {
"type": "object",
"properties": {
"test": {
"type": "string"
}
}
},
"JsonApiError": {
"type": "object",
"properties": {
"errors": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ErrorObject"
}
}
}
},
"ErrorObject": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"links": {
"type": "object",
"properties": {
"about": {
"type": "string",
"format": "uri"
},
"type": {
"type": "string",
"format": "uri"
}
}
},
"status": {
"type": "string"
},
"code": {
"type": "string"
},
"title": {
"type": "string"
},
"detail": {
// Orinigal: "string"
"type": "object"
},
"source": {
"type": "object",
"properties": {
"pointer": {
"type": "string"
},
"parameter": {
"type": "string"
},
"header": {
"type": "string"
}
}
},
"meta": {
"type": "object",
"additionalProperties": true
}
},
"required": [
"detail"
]
}
}
}
};

export default invalidApiDocumentItemsDetailType;
Loading

0 comments on commit d8372b5

Please sign in to comment.