Open VManolas opened 8 years ago
Great, we can do something like: rules can be litteral string values of json schema (validators), or an array of previous two (interpreted as AND expression)
{
"request": {
"url": "article"
},
"response": {
"rules": {
"/data/0/id": "10",
"/data/0/attributes/slug": {
"type": "string",
"pattern": "/^[a-z-A-Z_\\-]+$/"
},
"/data/0/type": [
"article",
{
"type": "string",
"enum": [
"article"
]
}
]
}
}
}
Could you please explain more the "response" ? I can understand what it means, line by line, based on https://tools.ietf.org/html/rfc6901, but I cannot understand what it does. Thank you.
So let's say the above GET article
request, will return a collection of articles
like the following:
{
"data": [
{
"id": "10",
"type": "article",
"attributes": {
"slug": "blog-post",
"title": "Blog post"
}
},
{
"id": "33",
"type": "article",
"attributes": {
"slug": "hello-world",
"title": "Hello world, our first post!!"
}
}
]
}
We will test the above response against the defined response rules
/data/0/id
says go to data
go to first array item (0 is the array index) get the id
property, our rule is a literal string. So the id
should be the same value "10"
as it is specified./data/0/attributes/slug
again we are following the path inside the first item of data array, inside attributes
property and we the slug
property, this time we have json schema (validator) instead of a literal value to test against. So slug
as specified in that schema should be a string and have a pattern ^[a-z-A-Z_\-]+$/
in order to be valid.Basically if you wanted to write a rule for slut in a resource response you had to write:
{
"type": "object",
"properties": {
"data": {
"type": "object",
"properties": {
"attributes": {
"type": "object",
"properties": {
"slug": {
"type": "string"
}
}
}
}
}
}
}
and by using JSON pointers you only need to write:
{
"/data/attributes/slug": {
"type": "string"
}
}
Assuming data
and attributes
are objects
But the ruleObjects can test if data and attributes in correct schema. Anyway the pointer SHOULD throw an incorrect type exception if they are not objects.
Added at ff24ee15435b57307b9a20eeac12d9433b18985c
WIP example:
{
"meta": {
"description": "Get posts",
"order": -1,
"incomplete": false
},
"request": {
"url": "http://jsonplaceholder.typicode.com/posts",
"method": "GET",
"headers": [
"Accept: application/json"
]
},
"response": {
"statusCode": 200,
"headers": {
"Content-Type": "application/json; charset=utf-8"
},
"rules": {
"/0/userId" : {
"type": "integer"
},
"/0" : {
"type": "object"
},
"/0/title" : {
"type": "string"
},
"/0/id" : 1
}
}
}
NOTE perhaps we should allow multiple rules for the same path, in that case /response/rules must be an array with object elements witch specifies path and rule members.
in order to write rules for one path. (https://tools.ietf.org/html/rfc6901)