Data validation API example to validate POST json request against Json Schema with go lang.
- Clone the repo
- go get -v && go build -o api
- ./api --port 8080
Make a valid request:
curl -X POST \
http://localhost:8080/api/person \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-d '{
"firstName": "Go",
"lastName": "lang"
}'
response would be:
{
"success": true,
"errors": {}
}
Invalid request:
curl -X POST \
http://localhost:8080/api/person \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-d '{
"noName": "Go",
"lastName": "lang"
}'
response:
{
"success": false,
"errors": {
"firstName": "firstName is required"
}
}
go test -v