- Array Primitives
- Auth Primitives
- Bool Primitives
- Color Primitives
- DateTime Primitives
- Encoding Primitives
- JSON Primitives
- Object Primitives
- String Primitives
- Number Primitives
- Network Primitives
- Person Primitives
- Utility Primitives
- Error Codes
- More Examples
Validates and sanitizes arrays, applying a schema validator to each element.
import { ArrayValSan } from 'valsan'; // from 'valsan/array'
const emailListValidator = new ArrayValSan({
schema: new EmailValidator()
});
const result = await emailListValidator.run([
'user1@example.com',
'user2@example.com'
]);
// result.success === true
// result.data === ['user1@example.com', 'user2@example.com']
// Validation error in array element
const fail = await emailListValidator.run([
'valid@example.com',
'invalid-email'
]);
// fail.success === false
// fail.errors[0].field === '[1]' (array index)
// Optional array
const optional = new ArrayValSan({
schema: new IntegerValidator(),
isOptional: true
});
const result2 = await optional.run(undefined);
// result2.success === true
// result2.data === undefined
// Array of objects with nested validation
const addressValidator = new ArrayValSan({
schema: new ObjectValSan({
schema: {
street: new TrimSanitizer(),
city: new TrimSanitizer(),
zipCode: new PatternValidator({ pattern: /^\d{5}$/ })
}
})
});import { StringToBooleanValSan } from 'valsan'; // from 'valsan/bool'
const validator = new StringToBooleanValSan();
const result = await validator.run('yes');
// result.success === true
// result.data === true
// Default true values: 'true', '1', 'yes', 'on' (case-insensitive)
// Default false values: 'false', '0', 'no', 'off' (case-insensitive)
// Custom values
const custom = new StringToBooleanValSan({
trueValues: ['y', 'yes'],
falseValues: ['n', 'no']
});Validates hexadecimal color codes. Supports 3-digit (#RGB), 4-digit (#RGBA), 6-digit (#RRGGBB), and 8-digit (#RRGGBBAA) formats. Input is normalized to uppercase.
import { HexColorValSan } from 'valsan'; // from 'valsan/color'
const validator = new HexColorValSan();
// Valid 6-digit hex color
const result = await validator.run('#FF0000');
// result.success === true
// result.data === '#FF0000'
// Valid 3-digit short format
const shortResult = await validator.run('#F00');
// shortResult.success === true
// shortResult.data === '#F00'
// Valid 8-digit with alpha channel
const alphaResult = await validator.run('#FF0000FF');
// alphaResult.success === true
// alphaResult.data === '#FF0000FF'
// Valid 4-digit short format with alpha
const alphaShortResult = await validator.run('#F00F');
// alphaShortResult.success === true
// alphaShortResult.data === '#F00F'
// Lowercase is converted to uppercase
const lowerResult = await validator.run('#ff0000');
// lowerResult.success === true
// lowerResult.data === '#FF0000'
// Whitespace is trimmed
const trimResult = await validator.run(' #FF0000 ');
// trimResult.success === true
// trimResult.data === '#FF0000'
// Mixed case is normalized
const mixedResult = await validator.run('#FfAa00');
// mixedResult.success === true
// mixedResult.data === '#FFAA00'
// Invalid: missing hash
const fail = await validator.run('FF0000');
// fail.success === false
// fail.errors[0].code === 'hex_color'
// Invalid: invalid characters
const fail2 = await validator.run('#GG0000');
// fail2.success === false
// fail2.errors[0].code === 'hex_color'
// Invalid: wrong length (5 digits)
const fail3 = await validator.run('#FF000');
// fail3.success === false
// fail3.errors[0].code === 'hex_color'import { StringToDateValSan } from 'valsan'; // from 'valsan/date-time'
const validator = new StringToDateValSan();
const result = await validator.run('2024-01-15');
// result.success === true
// result.data instanceof Date === trueValidates and sanitizes input as an ISO 8601 timestamp string. Accepts Date or string input. Returns a valid ISO 8601 string if possible.
import { Iso8601TimestampValSan } from 'valsan'; // from 'valsan/date-time'
const validator = new Iso8601TimestampValSan();
const result = await validator.run('2024-01-15T12:34:56Z');
// result.success === true
// result.data instanceof Date === trueValidates that a string is a valid UUID (RFC 4122). Supports UUID v1, v3, v4, and v5 formats. Input is normalized to lowercase.
import { UuidValSan } from 'valsan'; // from 'valsan/encoding'
const validator = new UuidValSan();
const result = await validator.run('550e8400-e29b-41d4-a716-446655440000');
// result.success === true
// result.data === '550e8400-e29b-41d4-a716-446655440000'
// Invalid UUID
const fail = await validator.run('550e8400e29b41d4a716446655440000');
// fail.success === false
// fail.errors[0].code === 'uuid'Validates that a string is a valid semantic version (SemVer 2.0.0). Supports major.minor.patch with optional prerelease and build metadata.
import { SemverValSan } from 'valsan'; // from 'valsan/encoding'
const validator = new SemverValSan();
// Valid semantic version
const result = await validator.run('1.0.0');
// result.success === true
// result.data === '1.0.0'
// With prerelease identifier
const preResult = await validator.run('1.0.0-alpha');
// preResult.success === true
// preResult.data === '1.0.0-alpha'
// With multiple prerelease identifiers
const multiPreResult = await validator.run('1.0.0-alpha.beta.1');
// multiPreResult.success === true
// multiPreResult.data === '1.0.0-alpha.beta.1'
// With build metadata
const buildResult = await validator.run('1.0.0+build.1');
// buildResult.success === true
// buildResult.data === '1.0.0+build.1'
// With both prerelease and build
const bothResult = await validator.run('2.0.0-rc.1+build.123');
// bothResult.success === true
// bothResult.data === '2.0.0-rc.1+build.123'
// Trims whitespace
const trimResult = await validator.run(' 1.0.0 ');
// trimResult.success === true
// trimResult.data === '1.0.0'
// Invalid version with leading zeros
const fail = await validator.run('01.0.0');
// fail.success === false
// fail.errors[0].code === 'semver'Validates that a string is valid JSON (RFC 8259). Parses the JSON string and returns the parsed value (object, array, string, number, boolean, or null).
import { JsonValSan } from 'valsan'; // from 'valsan/json'
const validator = new JsonValSan();
// Valid JSON object
const result = await validator.run('{"key": "value"}');
// result.success === true
// result.data === { key: 'value' }
// Valid JSON array
const arrayResult = await validator.run('[1, 2, 3]');
// arrayResult.success === true
// arrayResult.data === [1, 2, 3]
// Valid JSON string
const stringResult = await validator.run('"hello"');
// stringResult.success === true
// stringResult.data === 'hello'
// Valid JSON number
const numberResult = await validator.run('42');
// numberResult.success === true
// numberResult.data === 42
// Valid JSON boolean
const boolResult = await validator.run('true');
// boolResult.success === true
// boolResult.data === true
// Valid JSON null
const nullResult = await validator.run('null');
// nullResult.success === true
// nullResult.data === null
// Complex nested JSON
const complexResult = await validator.run(
'{"users": [{"id": 1, "name": "John"}]}'
);
// complexResult.success === true
// complexResult.data === { users: [{ id: 1, name: 'John' }] }
// Trims whitespace
const trimResult = await validator.run(' {"key": "value"} ');
// trimResult.success === true
// trimResult.data === { key: 'value' }
// Invalid JSON
const fail = await validator.run('{"key": "value"');
// fail.success === false
// fail.errors[0].code === 'json'Validates and sanitizes objects, applying a schema of validators to object properties. Objects can be nested.
import { ObjectValSan } from 'valsan'; // from 'valsan/object'
// Basic object validation
const userValidator = new ObjectValSan({
schema: {
name: new TrimSanitizer(),
email: new EmailValidator(),
age: new IntegerValidator()
}
});
const result = await userValidator.run({
name: ' John Doe ',
email: 'john@example.com',
age: 30
});
// result.success === true
// result.data === { name: 'John Doe', email: 'john@example.com', age: 30 }
// Validation error in object property
const fail = await userValidator.run({
name: 'Jane',
email: 'invalid-email',
age: 25
});
// fail.success === false
// fail.errors[0].field === 'email'
// Nested objects
const addressValidator = new ObjectValSan({
schema: {
street: new TrimSanitizer(),
city: new TrimSanitizer(),
zipCode: new PatternValidator({ pattern: /^\d{5}$/ })
}
});
const userWithAddressValidator = new ObjectValSan({
schema: {
name: new TrimSanitizer(),
email: new EmailValidator(),
address: addressValidator
}
});
const result2 = await userWithAddressValidator.run({
name: ' John ',
email: 'john@example.com',
address: {
street: ' 123 Main St ',
city: ' Springfield ',
zipCode: '12345'
}
});
// result2.success === true
// result2.data.address.street === '123 Main St'
// Allow additional properties
const permissiveValidator = new ObjectValSan({
schema: {
name: new TrimSanitizer()
},
allowAdditionalProperties: true
});
const result3 = await permissiveValidator.run({
name: 'John',
metadata: { key: 'value' },
count: 42
});
// result3.success === true
// result3.data.metadata === { key: 'value' }
// result3.data.count === 42
// Optional object
const optional = new ObjectValSan({
schema: {
name: new TrimSanitizer()
},
isOptional: true
});
const result4 = await optional.run(undefined);
// result4.success === true
// result4.data === undefinedValidates that a string contains only alphabetic characters (letters only).
Spaces can optionally be allowed by using the allowSpaces option.
import { AlphaValidator } from 'valsan';
const validator = new AlphaValidator();
const result = await validator.run('hello');
// result.success === true
// result.data === 'hello'
// With numbers (fails)
const fail = await validator.run('abc123');
// fail.success === false
// fail.errors[0].code === 'alpha'
// Allow spaces
const validatorWithSpaces = new AlphaValidator({ allowSpaces: true });
const result2 = await validatorWithSpaces.run('hello world');
// result2.success === true
// result2.data === 'hello world'
// With numbers when spaces allowed (still fails)
const fail2 = await validatorWithSpaces.run('hello 123');
// fail2.success === false
// fail2.errors[0].code === 'alpha'Validates that a string contains only alphanumeric characters (letters and numbers).
import { AlphanumericValidator } from 'valsan';
const validator = new AlphanumericValidator();
const result = await validator.run('abc123');
// result.success === true
// result.data === 'abc123'
const fail = await validator.run('abc-123');
// fail.success === false
// fail.errors[0].code === 'STRING_NOT_ALPHANUMERIC'
// Custom error message
const custom = new AlphanumericValidator({ errorMessage: 'Only letters and numbers allowed!' });
const fail2 = await custom.run('abc-123');
// fail2.success === false
// fail2.errors[0].message === 'Only letters and numbers allowed!'Validates and sanitizes strings to slug format (lowercase, alphanumeric with hyphens). Optionally converts strings to valid slug format.
import { SlugValSan } from 'valsan';
const validator = new SlugValSan();
const result = await validator.run('hello-world');
// result.success === true
// result.data === 'hello-world'
// Uppercase fails
const fail = await validator.run('Hello-World');
// fail.success === false
// fail.errors[0].code === 'slug'
// Spaces are not allowed
const fail2 = await validator.run('hello world');
// fail2.success === false
// fail2.errors[0].code === 'slug'
// Auto-convert to slug format
const autoValidator = new SlugValSan({ autoConvert: true });
const result2 = await autoValidator.run('Hello World');
// result2.success === true
// result2.data === 'hello-world'
// Underscores are converted to hyphens
const result3 = await autoValidator.run('hello_world_test');
// result3.success === true
// result3.data === 'hello-world-test'
// Special characters are removed
const result4 = await autoValidator.run('Hello@World!');
// result4.success === true
// result4.data === 'helloworld'Removes leading and trailing whitespace from strings.
import { TrimSanitizer } from 'valsan';
const validator = new TrimSanitizer();
const result = await validator.run(' hello ');
// result.success === true
// result.data === 'hello'Converts strings to lowercase.
import { LowercaseSanitizer } from 'valsan';
const validator = new LowercaseSanitizer();
const result = await validator.run('HELLO');
// result.success === true
// result.data === 'hello'Converts strings to uppercase.
import { UppercaseSanitizer } from 'valsan';
const validator = new UppercaseSanitizer();
const result = await validator.run('hello');
// result.success === true
// result.data === 'HELLO'Validates that a string meets a minimum length requirement.
import { MinLengthValidator } from 'valsan';
const validator = new MinLengthValidator({ minLength: 3 });
const result = await validator.run('hi');
// result.success === false
// result.errors[0].code === 'string_min_len'Validates that a string does not exceed a maximum length.
import { MaxLengthValidator } from 'valsan';
const validator = new MaxLengthValidator({ maxLength: 10 });
const result = await validator.run('this is way too long');
// result.success === false
// result.errors[0].code === 'string_max_len'Validates that a string's length is between a minimum and maximum (inclusive).
import { LengthValidator } from 'valsan';
const validator = new LengthValidator({ minLength: 2, maxLength: 5 });
const result = await validator.run('abcd');
// result.success === true
// result.data === 'abcd'
const failShort = await validator.run('a');
// failShort.success === false
// failShort.errors[0].code === 'string_min_len'
const failLong = await validator.run('abcdef');
// failLong.success === false
// failLong.errors[0].code === 'string_max_len'Validates that a string matches a regular expression pattern.
import { PatternValidator } from 'valsan';
const validator = new PatternValidator({
pattern: /^\d{3}-\d{4}$/,
errorMessage: 'Must be in format: XXX-XXXX'
});
const result = await validator.run('123-4567');
// result.success === trueConverts a string to a number, validating that it's a valid numeric string.
import { StringToNumberValSan } from 'valsan'; // from 'valsan/number'
const validator = new StringToNumberValSan();
const result = await validator.run('42');
// result.success === true
// result.data === 42 (number type)Validates that a number is a decimal (has decimal places). Optionally validates the number of decimal places.
import { DecimalValidator } from 'valsan'; // from 'valsan/number'
const validator = new DecimalValidator();
const result = await validator.run(3.14);
// result.success === true
// result.data === 3.14
// Integers are rejected
const fail = await validator.run(42);
// fail.success === false
// fail.errors[0].code === 'decimal'
// With max decimal places
const validator2 = new DecimalValidator({ maxDecimalPlaces: 2 });
const result2 = await validator2.run(3.14);
// result2.success === true
// result2.data === 3.14
const fail2 = await validator2.run(3.14159);
// fail2.success === false
// fail2.errors[0].code === 'max_decimal_places'
// With exact decimal places
const validator3 = new DecimalValidator({ decimalPlaces: 2 });
const result3 = await validator3.run(3.14);
// result3.success === true
// result3.data === 3.14
const fail3 = await validator3.run(3.1);
// fail3.success === false
// fail3.errors[0].code === 'exact_decimal_places'Validates that a number meets a minimum value requirement.
import { MinValidator } from 'valsan';
const validator = new MinValidator({ min: 0 });
const result = await validator.run(-5);
// result.success === false
// result.errors[0].code === 'minimum'Validates that a number does not exceed a maximum value.
import { MaxValidator } from 'valsan';
const validator = new MaxValidator({ max: 100 });
const result = await validator.run(150);
// result.success === false
// result.errors[0].code === 'maximum'Validates that a number falls within a specified range.
import { RangeValidator } from 'valsan';
const validator = new RangeValidator({ min: 0, max: 100 });
const result = await validator.run(50);
// result.success === trueValidates that a number is an integer (no decimal places).
import { IntegerValidator } from 'valsan';
const validator = new IntegerValidator();
const result = await validator.run(3.14);
// result.success === false
// result.errors[0].code === 'integer'Validates that a string is a valid IPv4 or IPv6 address.
import { IpAddressValSan } from 'valsan'; // from 'valsan/network'
const validator = new IpAddressValSan();
const result = await validator.run('192.168.1.1');
// result.success === trueValidates that a string is a valid MAC address.
import { MacAddressValSan } from 'valsan'; // from 'valsan/network'
const validator = new MacAddressValSan();
const result = await validator.run('00:1A:2B:3C:4D:5E');
// result.success === trueValidates that a value is a valid TCP/UDP port number (0-65535).
import { PortNumberValSan } from 'valsan'; // from 'valsan/network'
const validator = new PortNumberValSan();
const result = await validator.run(8080);
// result.success === trueValidates that a string is a valid URL.
import { UrlValSan } from 'valsan'; // from 'valsan/network'
const validator = new UrlValSan();
const result = await validator.run('https://example.com');
// result.success === trueValidates that a string is a valid fully qualified domain name (FQDN).
import { FqdnValSan } from 'valsan'; // from 'valsan/network'
const validator = new FqdnValSan();
const result = await validator.run('example.com');
// result.success === trueValidates that a string is a valid email address, with options for allowed domains and plus addressing.
import { EmailValidator } from 'valsan'; // from 'valsan/person'
const validator = new EmailValidator();
const result = await validator.run('test@example.com');
// result.success === trueValidates that a string is a valid HTTP Bearer token (RFC 6750).
import { BearerTokenValSan } from 'valsan';
const validator = new BearerTokenValSan();
const result = await validator.run('mF_9.B5f-4.1JqM');
// result.success === true
// result.data === 'mF_9.B5f-4.1JqM'
const fail = await validator.run('Bearer ');
// fail.success === false
// fail.errors[0].code === 'valid_bearer_token'Validates that a value is one of a set of allowed values.
import { EnumValidator } from 'valsan'; // from 'valsan/utility'
const validator = new EnumValidator({ allowedValues: ['red', 'green', 'blue'] });
const result = await validator.run('red');
// result.success === trueAll primitives use consistent, descriptive error codes:
array- Input is not an arrayrequired- Array is required but was not provided
object- Input is not a valid objectrequired- Object is required but was not providedunexpected_field- Object contains a field not defined in schema
boolean- String is not a recognized boolean value
hex_color- Not a valid hex color formatstring- Input is not a string
date- String cannot be converted to a valid dateiso8601- Not a valid ISO 8601 timestamp
uuid- Not a valid UUID formatsemver- Not a valid semantic version formatstring- Input is not a string
json- Input is not valid JSONstring- Input is not a string
empty_string- String is empty when empty strings are not allowedstring_min_len- String is shorter than minimum lengthstring_max_len- String exceeds maximum lengthpattern- String doesn't match required patternstring- Input is not a stringemail_format- Not a valid email addressemail_domain- Email domain not allowed
number- String cannot be converted to a numberminimum- Number is less than minimum valuemaximum- Number exceeds maximum valuenumber_range- Number is outside the allowed rangeinteger- Number has decimal places when integer required
ip_address- Not a valid IPv4 or IPv6 addressmac- Not a valid MAC addressport_number- Not a valid port_number numberurl- Not a valid URLfqdn- Not a valid FQDN
enum- Value is not in the allowed set
See the examples directory for more comprehensive usage examples:
examples/index.ts- Basic composed validator examplesexamples/primitives.ts- Showcase of all primitive validators