jc21 / cypress-jsonschema-validation

MIT License
1 stars 2 forks source link

Unable to verify api response with Json schema #1

Open hassaniqbalKhan opened 3 years ago

hassaniqbalKhan commented 3 years ago

I am trying to use your plugin in cypress for validating api response but I am unable to figure out that why it is always returning should("equal",null). I want to capture the response of the validator that whether it is validating the schema or not? Do we need anything else in this to verify the schema? Following is the sample code for your reference: it('Should return a valid payload', function () { cy.api({ url: '' + Cypress.env('baseUrlapi') + '/api/generalTest, headers: { "Content-Type": "application/json;charset=UTF-8", "Origin": "" + Cypress.config().baseUrl + "" } }).then($response => { cy.task('validateJsonSchema', { data: $response.body, verbose: true, // optional, default: false // // Or you can also define the schema inline: schema: { "type": "object", "properties": { "status": { "type": "integer" }, "message": { "type": "string" }, "body": { "type": "object" } } }, }).should('equal', null); }); });

it always give null to equal and it is not validating the actual schema.

jc21 commented 3 years ago

I want to capture the response of the validator that whether it is validating the schema or not?

Yep, the .should('equal', null); line is asserting the response there. The response will either be a string or null. If the validation is successful then the response is null because there's no errors to report. When the validation fails, this assertion fails and the test results will explain why using the validation result in the string. Here's an example from one of my failures:

AssertionError: expected 'data.result.state should be equal to one of the allowed values' to equal null
hassaniqbalKhan commented 3 years ago

Hi thanks for your response! But still I am unable to figure out that if I have changed by json file from which I am validating my api response, the validator is giving the expect result i.e .should("equal", null) and this is passing my test case even if my data is changed. For example: there were some fields whose response will be a boolean, but I have changed them to string or number in my schema file but still the test case passed. So please can you explain that how everything is working and test case for schema validation is still passing for wrong data types.

jc21 commented 3 years ago

I've added a full example to this project: https://github.com/jc21/cypress-jsonschema-validation/tree/master/example

Have a play with that, the 2 test cases correctly pick up the string vs boolean types. See if you can replicate your situation in this cleanish setup.

hassaniqbalKhan commented 3 years ago

Thanks so much. I am getting following error while cloning your example... mac@Macs-MacBook-Pro newTest % git clone git@github.com:jc21/cypress-jsonschema-validation.git Cloning into 'cypress-jsonschema-validation'... The authenticity of host 'github.com (13.234.210.38)' can't be established. RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'github.com,13.234.210.38' (RSA) to the list of known hosts. git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.