Check variables against a definitions. If it doesn't mach throw a type error with a helpful message to see what is wrong.
def
- Can be a string where value of the string is the type that target needs to have.
- Can be a number or a boolean where value of def will have to match with the value of target.
- Can be an Array with one item of any type in this list. Target have to be an array with all items in it matching with that definition.
- Can be an Object with mandatory field definitions where key is the name of that field and value of key should be any type in this list.
target Target to match against to definition
const ds = {
screenLayout: 'layout-1',
treatment: 'url',
items: [1, '2', false],
gridLink: {
id: 'id',
type: 'type-1'
},
synopsis: 'You should check these out!'
}
const definition = {
screenLayout: 'String',
background: 'String',
items: ['Number|String'],
gridLink: {
id: 'String',
type: 'String'
}
};
expect(definition, data);
/* Throws TypeError
Target expected to match with {
"screenLayout": "String",
"background": "String <- Mismatch here Target expected to be String but it is undefined",
"items": [
"Number|String"
],
"gridLink": {
"id": "String",
"type": "String"
}
} but it is {
"screenLayout": "layout-1",
"treatment": "url",
"items": [
1,
"2",
false
],
"gridLink": {
"id": "id",
"type": "type-1"
},
"synopsis": "You should check these out !"
}
If you fix that it will throw TypeError again:
Target expected to have {
"screenLayout": "String",
"items": "Number|String <- Mismatch here Target expected to be Array<of Number|String> but at index 2 it is boolean",
"gridLink": {
"id": "String",
"type": "String"
}
} but it is {
"screenLayout": "layout-1",
"treatment": "url",
"items": [
1,
"2",
false
],
"gridLink": {
"id": "id",
"type": "type-1"
},
"synopsis": "You should check these out !"
}
*/