go – How to implement a custom json validator-ThrowExceptions

Exception or error:

I need to test my system with lots of scenarios. For each scenario I will define the request and expected response, then I will make the request and compare the returned response and expected response.

For example, the REST API /add return a+b.

request:

{
    "a":1,
    "b":2
}

expected response (validator style description, could be something else since I don’t know if there is any better solution):

{
    "err_code":"int, required, eq=0"
    "data":"int, required, eq=3"
}

returned response:

success
{
    "err_code":0,
    "data":3
}

failure
{
    "err_code":500,
    "data":0
}

So my question here is, how to implement a custom json validator with some struct/fied/type/value description, or is there any better solution?

go-playground/validator needs to define everything at struct level, what I need is to check if a json conforms to my descriptive json (something as above).

How to solve:

Finally I used regular expression to implement my simple json validator,here is my repo https://github.com/seaguest/jsonvalidator

Still a lot of things need to be done, but it does meet my needs right now.

Leave a Reply

Your email address will not be published. Required fields are marked *