Documentation
¶
Overview ¶
Example (Simple) ¶
package main
import (
"errors"
"fmt"
"slices"
"github.com/scriptnull/jsonseal"
)
var paymentRequestWithInsufficientFunds = []byte(`
{
"account_id": "3ee7b5eb-f3fc-4f0b-9e01-8d7a0fa76f0b",
"balance": 15,
"currency": "USD",
"payment": {
"amount": 50,
"currency": "USD",
"mode": "neft"
}
}
`)
type SimplePaymentRequest struct {
AccountID string `json:"account_id"`
Balance float64 `json:"balance"`
Currency Currency `json:"currency"`
Payment struct {
Amount float64 `json:"amount"`
Currency Currency `json:"currency"`
Mode PaymentMode `json:"mode"`
} `json:"payment"`
}
func (r *SimplePaymentRequest) Validate() error {
var payment jsonseal.CheckGroup
payment.Check(func() error {
if r.Payment.Currency != r.Currency {
return errors.New("payment not allowed to different currency")
}
if r.Payment.Amount > r.Balance {
return errors.New("insufficient balance")
}
return nil
})
payment.Field("payment.mode").Check(func() error {
if !slices.Contains(SupportedPaymentModes, r.Payment.Mode) {
return fmt.Errorf(`"%s" is unsupported`, r.Payment.Mode)
}
return nil
})
return payment.Validate()
}
func main() {
var paymentRequest SimplePaymentRequest
err := jsonseal.Unmarshal(paymentRequestWithInsufficientFunds, &paymentRequest)
if err != nil {
fmt.Println("Plain error")
fmt.Print(err)
fmt.Println()
fmt.Println("JSON error")
fmt.Println(jsonseal.JSONFormat(err))
fmt.Println()
fmt.Println("JSON error with indent")
fmt.Println(jsonseal.JSONIndentFormat(err, "", " "))
fmt.Println()
return
}
}
Output: Plain error insufficient balance payment.mode "neft" is unsupported JSON error {"errors":[{"error":"insufficient balance"},{"fields":["payment.mode"],"error":"\"neft\" is unsupported"}]} JSON error with indent { "errors": [ { "error": "insufficient balance" }, { "fields": [ "payment.mode" ], "error": "\"neft\" is unsupported" } ] }
Index ¶
- func JSONFormat(e error) string
- func JSONIndentFormat(e error, prefix string, indent string) string
- func Unmarshal(data []byte, v any) error
- func UnmarshalValidate(data []byte, v Validator) error
- func Validate(v Validator) error
- type CheckGroup
- type Decoder
- func (dec *Decoder) Buffered() io.Reader
- func (dec *Decoder) Decode(v any) error
- func (dec *Decoder) DecodeValidate(v Validator) error
- func (dec *Decoder) DisallowUnknownFields()
- func (dec *Decoder) InputOffset() int64
- func (dec *Decoder) More() bool
- func (dec *Decoder) Token() (json.Token, error)
- func (dec *Decoder) UseNumber()
- func (dec *Decoder) WithUnknownFieldSuggestion() *Decoder
- type Error
- type Errors
- type FieldChain
- type Validator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JSONFormat ¶
func Unmarshal ¶
Unmarshal is a drop-in replacement for the standard library json.Unmarshal But performs jsonseal validations if the input implements the jsonseal.Validator interface
func UnmarshalValidate ¶ added in v0.3.0
UnmarshalValidate is like jsonseal.Unmarshal but helps to ensure that the input implements the jsonseal.Validator interface at compile time
Types ¶
type CheckGroup ¶
type CheckGroup struct {
// contains filtered or unexported fields
}
CheckGroup is collection of checker functions that contain validaton rules
func (*CheckGroup) Check ¶
func (v *CheckGroup) Check(validate func() error)
func (*CheckGroup) Field ¶
func (v *CheckGroup) Field(f string) *FieldChain
func (*CheckGroup) Fieldf ¶
func (v *CheckGroup) Fieldf(f string, a ...any) *FieldChain
func (*CheckGroup) Validate ¶
func (v *CheckGroup) Validate() error
type Decoder ¶ added in v0.2.0
type Decoder struct {
// contains filtered or unexported fields
}
Decoder is a drop-in replacement for the standard library json.Decoder
func NewDecoder ¶ added in v0.2.0
func (*Decoder) DecodeValidate ¶ added in v0.3.0
func (*Decoder) DisallowUnknownFields ¶ added in v0.2.0
func (dec *Decoder) DisallowUnknownFields()
func (*Decoder) InputOffset ¶ added in v0.2.0
func (*Decoder) WithUnknownFieldSuggestion ¶ added in v0.2.0
type Error ¶
func (*Error) MarshalJSON ¶
type FieldChain ¶
type FieldChain struct {
// contains filtered or unexported fields
}
func (*FieldChain) Check ¶
func (fc *FieldChain) Check(validate func() error)
Click to show internal directories.
Click to hide internal directories.