On the JavaScript target, decode.field (and probably decode.at and decode.subfield) succeed for keys inherited from Object.prototype (like toString and constructor). The Erlang target reports these as missing. The same Gleam source compiled for the two targets, therefore, produces different decoder results on the same input.
import gleam/dynamic/decode
import gleam/io
import gleam/json
import gleam/string
pub fn main() {
let decoder = {
use name <- decode.field("toString", decode.dynamic)
decode.success(string.inspect(name))
}
let value = json.parse("{}", decoder)
io.println(string.inspect(value))
// JS: Ok("//fn() { ... }")
// Erlang: Error(UnableToDecode([DecodeError("Field", "Nothing", ["toString"])]))
}
Using Object.hasOwn instead of the in keyword should prevent this.
On the JavaScript target,
decode.field(and probablydecode.atanddecode.subfield) succeed for keys inherited fromObject.prototype(liketoStringandconstructor). The Erlang target reports these as missing. The same Gleam source compiled for the two targets, therefore, produces different decoder results on the same input.Using
Object.hasOwninstead of theinkeyword should prevent this.