Skip to content

Home > @opengovsg/validators

validators package ​

A set of validators that provide sensible defaults to prevent common security vulnerabilities.

Classes ​

ClassDescription
OptionsErrorInvalid options error.
RelUrlValidatorParses URLs according to WHATWG standards and validates against a given origin.
UrlValidationErrorInvalid URL error.
UrlValidatorParses URLs according to WHATWG standards and validates against a whitelist of allowed protocols and hostnames, preventing open redirects, XSS, SSRF, and other security vulnerabilities.
WebhookUrlValidationErrorInvalid or unsafe webhook URL error.

Functions ​

FunctionDescription
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 ​

InterfaceDescription
EmailValidatorOptionsThe options to use for email validation.
PathValidatorOptionsThe options to use for path validation.
UrlValidatorOptionsThe options to use for URL validation.

Variables ​

VariableDescription
webhookUrlSchemaSchema 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.