# Classify data Classification expresses how a field should be handled at framework boundaries. It helps preserve intent when schemas are reused, but it is not whole-program tracking of every copy of a value. Use the [logging example]() to see the distinction: an email and document are personal, an API key is secret, and the handler logs schema-paired values instead of raw personal strings. ## Choose a class for a reason | Schema | Handler receives | Schema-aware log projection | Response policy | | ------------------------- | -------------------- | ------------------------------------- | ------------------------------------------------------------------- | | Unclassified field | Ordinary typed value | Declared value, within logging bounds | Allowed if declared | | `sensitivity: 'public'` | Ordinary typed value | Declared value, within logging bounds | Allowed if declared | | `sensitivity: 'personal'` | Ordinary typed value | `[personal]` | Allowed if declared; application controls who may receive it | | `sensitivity: 'secret'` | `Secret` | `[secret]` | Refused at startup unless explicitly exposed in the response schema | These three are the only classes. Any other `sensitivity` value, such as a misspelling in JavaScript or a value read from configuration, makes the builder throw a `TypeError` when the schema is declared, so the service does not start. `public` behaves exactly like no class: the value is written in full. It records that someone decided the field is not sensitive, where no class cannot tell a decision from an oversight. It is stated on each field, never on an object, array, record or union: `t.object(properties, { sensitivity: 'public' })` does not compile, and throws a `TypeError` from JavaScript, because a class on a container covers everything inside it, and a field added to it later would be covered without anyone deciding. Mark the enclosing object, array or record `personal` or `secret` when its structure is itself sensitive; it is then written as one marker. When only a record's keys are personal, such as a map keyed by email address, mark the key schema instead: `t.record(t.string({ maxLength: 254, sensitivity: 'personal' }), t.integer(), { maxProperties: 50 })`. Logs, `redact` and validation details then write each key as `[personal]#1`, `[personal]#2` and so on, by its position in the record, and the values under their own schema. A key schema cannot be `secret`, because a secret value is wrapped in a `Secret` and an object key is always a plain string; `t.record` throws a `TypeError`. A response writes its record's keys as the handler returns them. Classification also coarsens validation details inside the marked field. ```mermaid flowchart LR Input["Validated secret field"] --> Wrapper["Secret value"] Wrapper -->|ordinary formatting| Marker["Secret marker"] Wrapper -->|explicit expose call| Raw["Raw application value"] Raw --> Review["Application controls further use"] ``` A `Secret` formats as `[secret]` through ordinary string conversion, JSON serialization, and inspection. `expose(secret)` deliberately returns its raw value so an application can use it—for example, to call a credential verifier. Once exposed, that raw value is no longer protected by the wrapper. For a secret response, the response schema also needs `t.exposed(field, '')`. A deliberate token-issuing endpoint may need that capability. It should be a narrow response contract, not a reason to expose a shared account model everywhere. ## Keep the schema attached when logging In a handler, `request.classified('body')` pairs the validated body with its route schema. `request.classified('body', 'email')` pairs one declared field. Outside a request, `classified(schema, value)` from `@skeld/core/log` serves the same purpose. The logging example uses: ```ts request.log.info('preview requested', { email: request.classified('body', 'email'), apiKey: request.body.apiKey, input: request.classified('body'), }); ``` This excerpt runs inside the [preview handler](). The email becomes `[personal]`, the wrapped API key becomes `[secret]`, and the body is projected through its schema. Undeclared object keys are not copied into that projection. **A raw personal string still logs as a string.** Passing `request.body.email` directly as a field, or interpolating it into the message, exposes it. Personal values have no runtime wrapper. The logger bounds and escapes author strings; it cannot infer their meaning. Plain object and array fields require classification; unclassified ones are rejected by the logger's types and become `[unclassified]` at runtime. Errors have a separate bounded reader. Classification is not an invitation to log entire application objects: prefer the smallest useful fields. ## Require a class on every string With no class stated, a field is written in full, and nothing tells an author who forgot to mark an email. The server option `requireSensitivity: true` closes that gap for declared schemas: `listen` refuses every `t.string` in a route's `params`, `query`, `headers`, `cookies`, `body` and responses that states no class, record key schemas included. It rejects with `BootError` code `UNCLASSIFIED_FIELD`, naming the route, the part and the JSON pointer of the first such field: ```text Route signup (POST /signup): body schema at "/properties/nick": a string needs sensitivity: 'secret', 'personal' or 'public' ``` A string inside an object, array, record or union marked `personal` or `secret` needs no class of its own, because the container is written as one marker. Literals and unions of literals need none: their values are fixed by the contract. The option is off by default and takes a boolean; any other value is `INVALID_OPTION`. It costs nothing per request: the walk runs once per route at `listen`. What it guarantees: with it on, a validated part logged whole, through `request.classified` or with `classified(schema, value)` against a route's schema, writes no string whose class nobody stated. What it does not: - It reads declared schemas only. A value your code builds and logs as a plain field, or interpolates into a message, is written as given. - It checks strings, not numbers or booleans. A personal number, such as an age, still needs `sensitivity: 'personal'` by your own decision. - It cannot judge a class. A field marked `public` by mistake is written in full. - It covers the routes of the server it is set on. A schema used only with `classified(schema, value)` outside any route is not checked, nor are a worker's schemas. ## Choose public data independently of logging policy An email may be appropriate in an authenticated profile response and inappropriate in an operational log. Marking it personal does not prohibit the profile response or establish that its recipient is authorized. Build a response schema with an explicit field allowlist and check the caller's rights before returning it. See [contract design]() and [security boundaries](). **Next:** run the [logging example](). Reference: [Secret](), [redact](), and [logging limits]().