validators package
A set of validators that provide sensible defaults to prevent common security vulnerabilities.
Classes
| Class | Description |
|---|---|
| OptionsError | Invalid options error. |
| RelUrlValidator | Parses URLs according to WHATWG standards and validates against a given origin. |
| UrlValidationError | Invalid URL error. |
| UrlValidator | Parses URLs according to WHATWG standards and validates against a whitelist of allowed protocols and hostnames, preventing open redirects, XSS, SSRF, and other security vulnerabilities. |
| WebhookUrlValidationError | Invalid or unsafe webhook URL error. |
Functions
| Function | Description |
|---|---|
| createEmailSchema(options) | Create a schema that validates emails against RFC 5322 and a whitelist of domains. |
| createPathSchema(options) | Create a schema that validates user-supplied pathnames for filesystem operations. |
| createUrlSchema(options) | Create a schema that validates user-supplied URLs. This does the same thing as the UrlValidator class, but it returns a Zod schema which can be used as part of a larger schema. |
Interfaces
| Interface | Description |
|---|---|
| EmailValidatorOptions | The options to use for email validation. |
| PathValidatorOptions | The options to use for path validation. |
| UrlValidatorOptions | The options to use for URL validation. |
Variables
| Variable | Description |
|---|---|
| webhookUrlSchema | Schema for webhook destination URLs, for immediate client/server feedback (e.g. in a form or API input schema) on obvious blocked targets. Requires a real domain - z.httpUrl()'s hostname check already rejects every literal IP address (v4, v6, and obfuscated forms like decimal/octal/hex encoding or IPv4-mapped IPv6), so it also rejects localhost and single-label hostnames like the bare metadata cloud-metadata alias; *.localhost subdomains (RFC 6761) are blocked explicitly, since those are still valid-looking domains. Does not perform DNS resolution - use WebhookUrlValidator.fetch or WebhookUrlValidator.validateAsync (from the @opengovsg/validators/server/webhook-url subpath) for that. |