Ask a Question

Schema validation for offline schemas

nzStacy
Occasional Contributor

Schema validation for offline schemas

Hi, in Postman I can define a schema and the assert against that directly - how can I do this in Ready API

 

eg, the schema and then the assert

 

I see ready API has a URL to point to but I can't use it, so was wondering how I do this the same as postman

 

const schema = {
        "type":"array",
            "items":[
                {"type":"object",
                    "properties":{
                        "Id": {"type":"string"},
                        "Number": {"type":"string"},
                        "AK": {"type":"integer"},
                        "CategoryId": {"type":"integer"},
                        "Note": {"type":"string"},
                        "CreatedByUserId": {"type":"string"},
                        "CreatedDate": {"type":"string"},
                        "LastModifiedByUserId": {"type":"string"},
                        "LastModifiedDate": {"type":"string"}
                    },
                     "required":["Id", "Number", "AK", "CategoryId", "Note", "CreatedByUserId", "CreatedDate",  "LastModifiedByUserId", "LastModifiedDate"],
                     "additionalProperties": false}], }

 

 

 

pm.response.to.have.jsonSchema(schema);

 

 

Thanks

4 REPLIES 4
richie
Community Hero

Hey @nzStacy 

 

JSON Schema Compliance Assertion - have a look here -->  https://support.smartbear.com/readyapi/docs/testing/assertions/reference/compliance/json-schema.html

 

Cheers,

 

Rich

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
nzStacy
Occasional Contributor

@richie 

 

Thanks for the reply, at this stage I don;t have the ability to do any url based schema validation - I am going to need to investigate groovy scripting for a schema

Hey @nzStacy 

 

Sorry - to be clear - I'm not understanding - do you mean you're saying the "schema file" isn't published somewhere and you have it locally or do you mean something different?

 

Also - the "schema file" - what actually is it?  Are you talking about a .json schema? a swagger?  an OAS? a .wadl?

 

Cheers,

 

Rich

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
nzStacy
Occasional Contributor

@richiesorry if I wasn't clear

 

We do not have the ability to access the schema through an online url, they are not public facing yet

 

The current way I did it was to write the schema by hand in javascript in postman and then use the postman schema validation to to assert that the response from the postman call matched the schema supplied in code, the 2 postman tests below


pm.test("Response is ok(authorized)", function () {
    pm.response.to.have.status(200);
});

pm.test("Schema is valid", function () {
	const schema = {
        "type":"array",
            "items":[
                {"type":"object",
                    "properties":{
                        "Id": {"type":"string"},
                        "Number": {"type":"string"},
                        "AK": {"type":"integer"},
                        "CategoryId": {"type":"integer"},
                        "Note": {"type":"string"},
                        "CreatedByUserId": {"type":"string"},
                        "CreatedDate": {"type":"string"},
                        "LastModifiedByUserId": {"type":"string"},
                        "LastModifiedDate": {"type":"string"}
                    },
                    "required":["Id", "Number", "AK", "CategoryId", "Note", "CreatedByUserId", "CreatedDate",  "LastModifiedByUserId", "LastModifiedDate"],
                    "additionalProperties": false
            }],        
    };
    pm.response.to.have.jsonSchema(schema);
});

 

I was thinking I could use a groovy validator to do it and then just get the result and check that

 

 

cancel
Showing results for 
Search instead for 
Did you mean: