Skip to content

How to validate input unmarshal to a struct #58

Answered by vearutop
juandiegopalomino asked this question in Q&A
Discussion options

You must be logged in to vote

This is possible with help of few extra libraries, please check this code snippet extracted from github.com/swaggest/rest codebase.

This additional processing has performance cost, so please make sure to run benchmarks if you plan to use it in performance-sensitive places.

(I've slightly changed MyStruct so that there is no clash between default and required.)

https://go.dev/play/p/ZqthI23bfwU

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"net/url"
	"reflect"

	"github.com/santhosh-tekuri/jsonschema/v3"
	"github.com/swaggest/form/v5"
	jsonschema2 "github.com/swaggest/jsonschema-go"
	"github.com/swaggest/refl"
)

type MyStruct struct {
	Amount float64  `json:"amount" defa…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by vearutop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants