Skip to content

Commit

Permalink
v1.3.0
Browse files Browse the repository at this point in the history
- Improved error messages when not chained to an API response.
- Updated icons for disable validation message.
- Improved API documentation.
  • Loading branch information
sclavijosuero committed Oct 10, 2024
1 parent 184c735 commit 4982881
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ npm install -D cypress-ajv-schema-validator

#### `cy.validateSchema(schema, path)`

Validates the response body against the provided schema.
It is expected to be chained to an API response (from a `cy.request()` or `cy.api()`). It validates the response body against the provided schema.

##### Parameters

Expand Down Expand Up @@ -288,6 +288,11 @@ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file

## Changelog

### [1.3.0]
- Improved error messages when not chained to an API response.
- Updated icons for disable validation message.
- Improved API documentation.

### [1.2.0]
- Integration with bahmutov/cy-api and filiphric/cypress-plugin-api to show JSON violations directly in their outputs on the UI.
- New Cypress environment variable to disable schema validation.
Expand Down
Binary file modified images/disabled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cypress-ajv-schema-validator",
"version": "1.2.0",
"version": "1.3.0",
"description": "Lightweight API schema validator for Plain JSON schemas, Swagger schema documents, or OpenAPI schema documents using Ajv JSON Schema Validator",
"main": "src/index.js",
"scripts": {
Expand Down
23 changes: 17 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ const iconPropertyError = '🟠'
const iconPropertyMissing = '🟥'
const iconMoreErrors = '➕'

const warningDisableSchemaValidation = ` API SCHEMA VALIDATION DISABLED `
const warningDisableSchemaValidation = `⚠️ API SCHEMA VALIDATION DISABLED ⚠️`
const msgDisableSchemaValidation = '- The Cypress environment variable "disableSchemaValidation" has been set to true.'
const errorNoValidApiResponse = 'The element chained to the cy.validateSchema() command is expected to be an API response!'
const invalidSchemaParameter = `You must provide a valid schema!`
const errorInvalidSchemaParameters = `You must provide valid schema parameters (missing 'endpoint', 'method' or 'status' params)!`
const errorResponseBodyAgainstSchema ='The response body is not valid against the schema!'

// ------------------------------------
// PUBLIC CUSTOM COMMANDS
Expand Down Expand Up @@ -87,6 +91,11 @@ Cypress.Commands.add("validateSchema",

console.log(`${warningDisableSchemaValidation} ${msgDisableSchemaValidation}`)
} else {
// Check if it is a valid API Response object
if (response == null || !(response.hasOwnProperty('body') && response.hasOwnProperty('status') && response.hasOwnProperty('headers'))) {
console.log(errorNoValidApiResponse)
throw new Error(errorNoValidApiResponse)
}

const data = response.body

Expand Down Expand Up @@ -152,15 +161,15 @@ Cypress.Commands.add("validateSchema",
* });
*/
export const validateSchema = (data, schema, path) => {

if (Cypress.env('disableSchemaValidation') === true) {
// We need to check also here since validateSchema() is a public function
console.log(warningDisableSchemaValidation)
return null
}

if (schema == null) {
throw new Error(`You must provide a valid schema parameter!`);
console.log(errorInvalidSchema)
throw new Error(errorInvalidSchema)
}

if (path != null) {
Expand Down Expand Up @@ -238,7 +247,8 @@ export const validateSchema = (data, schema, path) => {
const _getSchemaFromSpecificationDoc = (schema, { endpoint, method, status }) => {

if (endpoint == null || method == null || status == null) {
throw new Error(`You must provide valid schema parameters (missing 'endpoint', 'method' or 'status' params)!`);
console.log(errorInvalidSchemaParameters)
throw new Error(errorInvalidSchemaParameters)
}

// Normalize the method to lowercase for Swagger and OpenAPI documents
Expand Down Expand Up @@ -465,7 +475,8 @@ const _logValidationResult = (data, errors, maxErrorsToShow = 10) => {

// Throw an error to fail the test
cy.then(() => {
throw new Error('The response body is not valid against the schema!')
console.log(errorResponseBodyAgainstSchema)
throw new Error(errorResponseBodyAgainstSchema)
})
}
}
Expand Down Expand Up @@ -547,4 +558,4 @@ const mustEnableMismatchesOnUI = () => {
const _random = () => {
return Math.random().toString(36).substring(10)

}
}

0 comments on commit 4982881

Please sign in to comment.