Optimize performance with manual sanitization
mainIn controlled environments like RESTful APIs, most fields are known to be safe. You can improve performance by setting the global unsafe option to 'manual' and explicitly marking only uncontrolled user-submitted fields for sanitization in the schema using { sanitize: true }.
When a field is marked with sanitize: true, the encoder will sanitize that specific string and continue encoding regardless of the global unsafe configuration.
import { Type as t } from '@sinclair/typebox'
import { createAccelerator } from 'json-accelerator'
const shape = t.Object({
name: t.String(),
id: t.Number(),
unknown: t.String({ sanitize: true }) // Explicitly mark as needing sanitization
})
const value = {
id: 0,
name: 'saltyaom',
unknown: `hello\nworld`
} satisfies typeof shape.static
// Set to 'manual' to skip automatic sanitization on all other fields
const encode = createAccelerator(shape, {
sanitize: 'manual'
})
console.log(encode(value)) // {"id":0,"name":"saltyaom","unknown":"hello\\nworld"}