package schema import ( "bytes" "encoding/json" "errors" "fmt" "io" "regexp" "sort" "strings" ) // The caps of custom JSON-LD (UI-SPEC C17, T-33-24). const ( MaxCustomBytes = 16 * 1024 MaxCustomDepth = 8 ) // The codes of a refused value; each is a phrase key under schema. const ( CodeInvalidJSON = "invalid_json" CodeNotObject = "not_object" CodeTooLarge = "too_large" CodeTooDeep = "too_deep" CodeScriptTag = "script_tag" CodeTypeUnknown = "type_unknown" CodeFieldInvalid = "field_invalid" ) // Error is a refused structured data value: a code, and for invalid JSON // the parser's message with its line and column; for a field the field // name. type Error struct { Code string Detail string Field string } func (e *Error) Error() string { if e.Detail != "" { return "schema: " + e.Code + ": " + e.Detail } return "schema: " + e.Code } // Value is the stored seo_schema value (UI-SPEC C17): the editor's type // ("" for the kind's default), the Service fields and the custom JSON-LD. type Value struct { Type string `json:"type"` Fields ServiceFields `json:"fields"` Custom string `json:"custom"` } // ParseValue reads a stored or submitted value; "" is the zero value. func ParseValue(raw string) (Value, error) { var v Value if strings.TrimSpace(raw) == "" { return v, nil } if err := json.Unmarshal([]byte(raw), &v); err != nil { return Value{}, &Error{Code: CodeInvalidJSON, Detail: err.Error()} } return v, nil } // scriptTag finds a literal