Create Validators
Use createStandardValidator and createSyncStandardValidator to build reusable validation functions.
When the same schema is reused many times, creating a validator function once makes code cleaner and easier to share.
Why Create Validator Functions?
Use create helpers when you want:
- a reusable validator for a specific schema
- separation between schema setup and runtime calls
- consistent behavior across multiple modules
createStandardValidator
Creates an async validator that works with both sync and async schemas.
import { createStandardValidator } from "@zap-studio/validation";
const validateUser = createStandardValidator(userSchema);
const result = await validateUser(input);
if (result.issues) {
// invalid
} else {
// result.value
}
const user = await validateUser(input, { throwOnError: true });Use it when:
- your call sites are async
- schema behavior might become async over time
- you want one reusable validator instance
createSyncStandardValidator
Creates a sync-only validator.
import { createSyncStandardValidator } from "@zap-studio/validation";
const validateUser = createSyncStandardValidator(userSchema);
const result = validateUser(input);
const user = validateUser(input, { throwOnError: true });Use it when:
- code must stay synchronous
- schemas are guaranteed sync
Important behavior:
- throws if the schema validates asynchronously
Which Create Helper Should I Use?
- Prefer
createStandardValidatorfor default package/app code. - Use
createSyncStandardValidatorin strict sync contexts only. - Pair create helpers with
standardValidate/standardValidateSyncdepending on whether you prefer reusable functions or direct one-shot validation calls.
Edit on GitHub
Last updated on