And I'm using xeipuuv/gojsonschema to receive a json file, this json file will be a future validator for other jsons that will be received, so it would be
1 - Receive a JSON, which is a jsonschema to validate a JSON
2 - When receiving a JSON input, validate with the jsonschema received in step 1 :
{ "object1": { "properties", }, "object2": { "properties", } }
However, I could not validate it because I always received the error that "object1" is necessary, and there is "object1"
And inside each object, there would be other validations like if there is a title, if there is an id, a birthdate
However, I could not validate it because I always received the error that "object1" is necessary, and there is "object1"
And inside each object, there would be other validations like if there is a title, if there is an id, a birthdate
Related
Let's say I have a Request data class:
data class Request(
val firstName: String,
val lastName: String
)
which I want to serialize when getting to a specific api route. Using Ktor, it would look like this:
call.receive<Request>()
This would work perfectly if I get a valid json such as { "firstName: "TestFirst", "lastName": "TestLast" }
But, what if we get a json object or array instead of the expected string? { "firstName: [], "lastName": {} }?
The library would throw an exception and I wouldn't be able to know we had two different validation problems:
firstName must be a valid string (and not an array)
lastName must be a valid string (and not an object).
How can I find these errors so that I would be able to map them nicely back to the user in the rest api response?
I've got a Go project, exposing REST CRUD APIs, for a Mongo collection. I'm using go-swagger to generate the swagger spec. However, I'm having trouble getting the JSON response to look like I want without breaking the go-swagger spec generator.
I'm trying to use go-swagger to generate a swagger-spec from annotations on go code. I'd like to see if I can make the response just be a JSON Array of Users, like below.
Is there a way to adjust the json annotation on the User struct to produce the desired result?
[
{"id": "5d8e9aaca00ef6123c989f69", "user_name": "zbeeblebrox"},
{"id": "5d8e9ab1a00ef6123c989f6a", "user_name": "another_user"}
]
Below is what I'm getting, understandably, a JSON object, containing key "data", with a value of an Array of User objects.
I've tried redefining the swagger response struct to be an alias of type []*User, which creates the right response body, but it breaks the go swagger generator.
{
"data": [
{"id": "5d8e9aaca00ef6123c989f69", "user_name": "zbeeblebrox"},
{"id": "5d8e9ab1a00ef6123c989f6a", "user_name": "another_user"}
]
}
Here's some code.
// swagger:model
type User struct {
Id *primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
UserName string `json:"user_name" bson:"user_name"`
}
// HTTP status code 200 and an array of repository models in data
//swagger:response usersResp
type swaggUsersResp struct {
// in:body
Data []*User `json:"data"`
}
I also tried doing it as an alias, which provides the desired JSON response, but breaks Go-Swagger code generation. I suspect this is because the swagger:response annotation is expected to be on a struct, not an alias.
// swagger:response usersResp
type swaggUsersResp = []*User
I'm trying to encode some JSON for a REST api, everything is working fine except for some errors. For example, with this struct:
type TemplateResponse struct {
Message string
Error error
Template Template
}
Encoded with this data:
res := TemplateResponse{"Template not found.", fmt.Errorf("There is no template on this host with the name " + vars["name"]), Template{}}
json.NewEncoder(w).Encode(res)
Returns:
{
"Message": "Template not found.",
"Error": {},
"Template": {
"Name": "",
"Disabled": false,
"Path": "",
"Version": ""
}
}
I'm getting this seemingly randomly across my application, where 'error' types are being returned as empty. Any ideas?
Thanks!
Because error is just an interface. It may hold a value of any concrete type that implements it.
In your example you used fmt.Errorf() to create an error value. That calls errors.New() which returns a pointer to a value of the unexported errors.errorString struct. Its definition is:
type errorString struct {
s string
}
This struct value will be marshaled, but since it has no exported fields (only exported fields are marshaled), it will be an empty JSON object: {}.
The "fix" is: don't marshal values of "general" interfaces, relying on that the dynamic values can be marshaled into JSON meaningfully. Instead you should add a field that stores the error string (the result of error.Error()), and omit the Error error field from marshaling, e.g.:
type TemplateResponse struct {
Message string
Error error `json:"-"`
ErrorMsg string
Template Template
}
Of course then you also need to set / fill the ErrorMsg field before marshaling.
Or if you don't need to store the error value in the struct, remove that field completely:
type TemplateResponse struct {
Message string
ErrorMsg string
Template Template
}
If you still want to keep the Error error field (and not the ErrorMsg field), then you need to implement a custom marshaling logic by implementing the json.Marshaler interface where you can "convert" the error value to a meaningful string for example (or into another value that can be marshaled properly).
I'm developing a server-side RESTful application which serves json data for its client applications. And I have to test so many various json outputs.
Each json has many properties and their validation methods are different like below a sample json.
So such my use-case, do you know good libraries or web services to test json data flexibly?
{
"system": { // data structure validation
"time": 1234566, // data type validation
"version": "0.0.1" // string matching validation
},
"app": { // data structure validation
"id": "1234", // string matching validation
"command": "do something", // string matching validation
"data": { // data structure validation
"hoge": "xxx", // data type validation
"fuga": 123 // data type validation
}
}
}
What do you mean by validating? validating the JSON structure or the data inside your JSON object?
To validate the structure you can parse it to another datatype like dictionary and see if you get any error while.
But to validate the data inside the object you need to validate each object in a very specific way for that object.
Use this link Just Place your JSON code and Test it. Quite easy.
i have the following JSON-GET response:
{
"check_lists": [
{
"id": 2,
"name": "Servicebesuch",
"description": ""
}]
}
Problem is the "check_lists" name which i have solved in one of the extract/extractSingle methods of the RESTSerializer so the data is loaded and deserialized successfully to the model named App.Checklist.
Now i got stucked again.
For serialization of my model-data to json, i have to do the same trick reverse but i can not find a hook for that. The serialize Method of the RESTSerializer gets only the pure record and the serialized json, received from this._super... call, does only hold the serialized object and not the call itself.
The server expects a PUT request (in case of updating a list) with the parameter packed in a param named check_list but ember sends it in a param named checklist.
Processing by CheckListsController#update as JSON
Parameters: {"checklist"=>{"name"=>"Kaffeevertrieb", "description"=>""},
"id"=>"3", "check_list"=>{}}
ActionController::ParameterMissing (param is missing or the value is empty: check_list):
Does someone know how to insert the underscore to the json request?
Greetings