bguard
Version:
**bguard** is a powerful, flexible, and type-safe validation library for TypeScript. It allows developers to define validation schemas for their data structures and ensures that data conforms to the expected types and constraints.
39 lines (36 loc) • 1.67 kB
text/typescript
import { RequiredValidation } from '../../core.mjs';
import '../../commonTypes.mjs';
import '../../InferType.mjs';
import '../../helpers/constants.mjs';
type DateTimeOptions = {
offset?: boolean;
precision?: number;
};
/**
* @description Asserts that a string value is a valid ISO 8601 datetime string.
* @param {DateTimeOptions} options Options to control the validation:
* - `offset`: If `true`, allows timezone offsets in the datetime string.
* - `precision`: Specify the exact number of fractional second digits allowed (e.g., 3 for milliseconds).
* @returns {RequiredValidation} A validation function that takes a received string and an exception context.
* @throws {ValidationError} if the received value is not a valid datetime string according to the options.
* @example
* const schema = string().custom(isValidDateTime());
* parseOrFail(schema, "2024-01-01T00:00:00Z"); // Valid
* parseOrFail(schema, "2024-01-01T00:00:00.123Z"); // Valid
* parseOrFail(schema, "2024-01-01T00:00:00+03:00"); // Invalid (no offsets allowed)
*
* const schemaWithOffset = string().custom(isValidDateTime({ offset: true }));
* parseOrFail(schemaWithOffset, "2024-01-01T00:00:00+04:00"); // Valid
*
* const schemaWithPrecision = string().custom(isValidDateTime({ precision: 3 }));
* parseOrFail(schemaWithPrecision, "2024-01-01T00:00:00.123Z"); // Valid
* parseOrFail(schemaWithPrecision, "2024-01-01T00:00:00.123456Z"); // Invalid
*
* @translation Error Translation Key = 's:isValidDateTime'
*/
declare const isValidDateTime: {
(options?: DateTimeOptions): RequiredValidation;
key: string;
message: string;
};
export { isValidDateTime };