MCPcopy
hub / github.com/pocketbase/pocketbase / PrepareValue

Method PrepareValue

core/field_json.go:119–148  ·  view source on GitHub ↗

PrepareValue implements [Field.PrepareValue] interface method.

(record *Record, raw any)

Source from the content-addressed store, hash-verified

117
118// PrepareValue implements [Field.PrepareValue] interface method.
119func (f *JSONField) PrepareValue(record *Record, raw any) (any, error) {
120 if str, ok := raw.(string); ok {
121 // in order to support seamlessly both json and multipart/form-data requests,
122 // the following normalization rules are applied for plain string values:
123 // - "true" is converted to the json `true`
124 // - "false" is converted to the json `false`
125 // - "null" is converted to the json `null`
126 // - "[1,2,3]" is converted to the json `[1,2,3]`
127 // - "{\"a\":1,\"b\":2}" is converted to the json `{"a":1,"b":2}`
128 // - numeric strings are converted to json number
129 // - double quoted strings are left as they are (aka. without normalizations)
130 // - any other string (empty string too) is double quoted
131 if str == "" {
132 raw = strconv.Quote(str)
133 } else if str == "null" || str == "true" || str == "false" {
134 raw = str
135 } else if ((str[0] >= '0' && str[0] <= '9') ||
136 str[0] == '-' ||
137 str[0] == '"' ||
138 str[0] == '[' ||
139 str[0] == '{') &&
140 is.JSON.Validate(str) == nil {
141 raw = str
142 } else {
143 raw = strconv.Quote(str)
144 }
145 }
146
147 return types.ParseJSONRaw(raw)
148}
149
150var emptyJSONValues = []string{
151 "null", `""`, "[]", "{}", "",

Callers 1

Calls 3

ParseJSONRawFunction · 0.92
QuoteMethod · 0.80
ValidateMethod · 0.65

Tested by 1