T28

Error

Types

T28 — A Json value read as if it were something else

Json is JSON as it arrives: it may be an object, a list, a string, a number, a boolean or null, so reading a field of it, indexing it, calling a method on it or adding to it assumes what nobody checked. Match its cases, or decode it into a type — User.decode(raw) — and read the value that comes back.

What the compiler said

error[T28]: `raw` is `Json`, and `.name` reads it as a record --> src/App.wf:5:10 | 5 | Text(raw.name) | ^^^ = help: JSON is read by matching its cases (`match raw { .object(fields) { … } }`) or decoding it into a type: `User.decode(raw)` = docs: https://webfluent.monzeromer.dev/errors/T28

The fix

Broken

wf

page P(path: "/", title: "Profile", description: "D") { state text = "{\"name\": \"Ada\"}" derived raw = JSON.parse(text) Heading("Profile").h1 Text(raw.name) }

Fixed

wf

type Person { name: String } page P(path: "/", title: "Profile", description: "D") { state text = "{\"name\": \"Ada\"}" derived raw = JSON.parse(text) derived name = match Person.decode(raw) { .ok(p) { p.name } .err(e) { e.message } } Heading("Profile").h1 Text(name) }

Decode it into a type — Person.decode(raw) is a result, .ok(person) or .err(e) naming where it went wrong — or match its cases (.object(fields), .array(items), .string(value), …). A connection says what it receives: socket chat = ws(…) { receive Message }, and on message(m) is then a Message.

Each example is a whole program: it is all of src/App.wf in a project with no other .wf file (trying an example).

Why this rule exists

Error.

Json is JSON as it arrives — JSON.parse, a connection that declares nothing it receives, a worker's answer. It may be an object, a list, a string, a number, a boolean or null, so reading .name of it, indexing it, calling a method on it or adding to it assumes what nobody checked. In the browser that is undefined where a name was meant, or NaN.

lints cannot lower it: it would ship a broken page.