forked from mtusi2026/mtusi2026lab6
102 lines
2.9 KiB
Go
102 lines
2.9 KiB
Go
// Package strictjson decodes objects with an exact set of required, non-null fields.
|
|
package strictjson
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"slices"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
// ErrInvalid indicates invalid JSON, encoding, fields or destination field types.
|
|
var ErrInvalid = errors.New("invalid JSON object")
|
|
|
|
// DecodeObject decodes one UTF-8 object with exactly the listed required fields.
|
|
// Duplicate, unknown, missing and null fields are rejected before unmarshaling.
|
|
// The destination must describe the supplied fields; this is not a general schema validator.
|
|
func DecodeObject(body []byte, target any, requiredFields ...string) error {
|
|
if !utf8.Valid(body) {
|
|
return ErrInvalid
|
|
}
|
|
if err := validateObject(body, requiredFields); err != nil {
|
|
return err
|
|
}
|
|
if err := json.Unmarshal(body, target); err != nil {
|
|
return ErrInvalid
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// validateObject enforces exact, unique, non-null required keys and one JSON value.
|
|
// All lab1 request properties are required; this is not a generic schema validator.
|
|
func validateObject(body []byte, requiredFields []string) error {
|
|
return validateSchema(body, requiredFields, requiredFields)
|
|
}
|
|
|
|
// validateSchema distinguishes required keys from optional allowed keys.
|
|
func validateSchema(body []byte, requiredFields, allowedFields []string) error {
|
|
decoder := json.NewDecoder(bytes.NewReader(body))
|
|
token, err := decoder.Token()
|
|
if err != nil || token != json.Delim('{') {
|
|
return ErrInvalid
|
|
}
|
|
seen := make(map[string]bool, len(requiredFields))
|
|
for decoder.More() {
|
|
key, err := readObjectField(decoder, allowedFields, seen)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
seen[key] = true
|
|
}
|
|
if token, err = decoder.Token(); err != nil || token != json.Delim('}') {
|
|
return ErrInvalid
|
|
}
|
|
for _, key := range requiredFields {
|
|
if !seen[key] {
|
|
return ErrInvalid
|
|
}
|
|
}
|
|
var trailing any
|
|
if err := decoder.Decode(&trailing); err != io.EOF {
|
|
return ErrInvalid
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// readObjectField reads a known, unique, non-null JSON object field.
|
|
func readObjectField(decoder *json.Decoder, requiredFields []string, seen map[string]bool) (string, error) {
|
|
token, err := decoder.Token()
|
|
if err != nil {
|
|
return "", ErrInvalid
|
|
}
|
|
key, ok := token.(string)
|
|
if !ok || seen[key] || !slices.Contains(requiredFields, key) {
|
|
return "", ErrInvalid
|
|
}
|
|
var value json.RawMessage
|
|
if err := decoder.Decode(&value); err != nil {
|
|
return "", ErrInvalid
|
|
}
|
|
if bytes.Equal(bytes.TrimSpace(value), []byte("null")) {
|
|
return "", ErrInvalid
|
|
}
|
|
return key, nil
|
|
}
|
|
|
|
// DecodeOptional rejects unknown, duplicate and null fields while allowing omitted optional fields.
|
|
func DecodeOptional(body []byte, target any, required, optional []string) error {
|
|
if !utf8.Valid(body) {
|
|
return ErrInvalid
|
|
}
|
|
allowed := append(append([]string{}, required...), optional...)
|
|
if err := validateSchema(body, required, allowed); err != nil {
|
|
return err
|
|
}
|
|
if err := json.Unmarshal(body, target); err != nil {
|
|
return ErrInvalid
|
|
}
|
|
return nil
|
|
}
|