totaljs/framework

How to validate the response schema from unit testing method?

LandyCuadra opened this issue · 5 comments

Is there a way to check the response fields name and type in an assertion like:

OK(response.value === someschema); // or another syntax it's just and example

so if someone change the structure of the object returned, the test fails.

I tried with builder.schema()... but it doesn't worked as I expected, it just cleaned my response object

any help would be appreciated.

Maybe you can prepare response data according to the schema via $MAKE() method in Total.js v3 - https://docs.totaljs.com/latest/en.html#api~global~%24MAKE and if the callback contains some error, then the test will fail.

We don't have any case where we return data from the schema same as defined fields in the schema. So in most cases, our schemas answer with a simple object { success: true, value: Object }.

yeah exactly out responses are like that but value: Object, I want to make sure it is returning the object we expected, just in case someone touch something there and change the return structure because we use mongodb projections to filter the response....

my doubt is $MAKE() does not return a boolean, so it can fire the assertion in a test?, will try anyway and let you know, thanks

ok, I just tried your solution, but $MAKE is not validating the structure is transforming it if it brings more fields than required, and nulling it if theres missing required fields, But for my test I don't need to transform the response but validate it, check if it brings the fields I am expecting and with the types I am expecting, if it doesn't match (because there are more or less fields, or a field is supossed to be a number and came a string), then fail the test

$MAKE() transforms an object according to the schema. If some required fields are invalid, you obtain err in the callback to capture something terrible.

It will be better if you manually compare values n the object.

great!, I will manage it that way, just wanted to see if there was another way, thank you!