Configuration
A great out-of-the-box experience is a core goal of ArkType, including safe defaults and helpful messages for complex errors.
However, it's equally important that when you need different behavior, you can easily configure it with the right granularity.
Level | Applies To | Example |
---|---|---|
default | built-in defaults for all Types | |
global | all Types parsed after the config is applied | |
scope | all Types parsed in the configured Scope | |
type | all Types shallowly referenced by the configured Type |
Some options only apply at specific levels, as reflected in the corresponding input types.
Use the `"arktype/config"` entrypoint if for global config!
If you need your config to apply to built-in keywords (important for options
like jitless
, numberAllowsNaN
, dateAllowsInvalid
), you should import and
configure
from "arktype/config"
before importing anything from
"arktype"
.
Otherwise, keywords will have already been parsed by the time your config applies!
Errors
To allow custom errors to be integrated seemlessly with built-in logic for composite errors (i.e. union
and intersection
), ArkType supports a set of composable options:
optional | description | example |
---|---|---|
description | ✅ a summary of the constraint that could complete the phrase "must be ___" 🥇 reused by other metadata and should be your first go-to for customizing a message | |
expected | ✅ a function accepting the error context and returning a string of the format "must be ___" ✅ specific to errors and takes precedence over | |
actual | ✅ a function accepting the data that caused the error and returning a string of the format "(was ___)" ✅ if an empty string is returned, the actual portion of the message will be omitted | |
problem | ✅ a function accepting the results of ❌ may not apply to composite errors like unions | |
message | ✅ a function accepting the result of ❌ may not apply to composite errors like unions |
By Code
Errors can also be configured by their associated code
property at a scope or global level.
For example:
ArkErrors
For use cases like i18n that fall outside the scope of this composable message config, the ArkErrors
array returned on validation failure contains ArkError
instances that can be discriminated via calls like .hasCode("divisor")
and contain contextual data specific to that error type as well as getters for each composable error part.
These ArkError
instances can be arbitrarily transformed and composed with an internationalization library. This is still a topic we're working on investigating and documenting, so please reach out with any questions or feedback!
Clone
By default, before a morph is applied, ArkType will deeply clone the original input value with a built-in deepClone
function that tries to make reasonable assumptions about preserving prototypes etc. The implementation of deepClone
can be found here.
You can provide an alternate clone implementation to the clone
config option.
To mutate the input object directly, you can set the clone
config option to false
.
onUndeclaredKey
Like TypeScript, ArkType defaults to ignoring undeclared keys during validation. However, it also supports two additional behaviors:
"ignore"
(default): Allow undeclared keys on input, preserve them on output"delete"
: Allow undeclared keys on input, delete them before returning output"reject"
: Reject input with undeclared keys
These behaviors can be associated with individual Types via the builtin "+"
syntax (see those docs for more on how they work). You can also change the default globally:
jitless
By default, when a Type
is instantiated, ArkType will precompile optimized validation logic that will run when the type is invoked. This behavior is disabled by default in environments that don't support new Function
, e.g. Cloudflare Workers.
If you'd like to opt out of it for another reason, you can set the jitless
config option to true
.