typebox-utils
Version:
TypeBox utilities with MongoDB ObjectId support and common validation types
88 lines (87 loc) • 4.12 kB
TypeScript
/**
* @package
*/
import { StaticDecode, StaticEncode, Type, type Static, type TSchema } from '@sinclair/typebox';
import { ObjectId } from 'mongodb';
/**
* Common reusable schema types
*/
declare const Utils: {
/**
* Unix timestamp type (milliseconds since
* @param {Object} config - Configuration object for the Timestamp type
* @param {number} [config.default] - Optional default value for the timestamp
* @param {number} [config.minimum] - Optional minimum value (defaults to 0)
* @param {number} [config.maximum] - Optional maximum value
* @returns {import('@sinclair/typebox').TNumber} A TypeBox schema for Unix timestamp
*/
readonly Timestamp: (config?: {
default?: number;
minimum?: number;
maximum?: number;
}) => import("@sinclair/typebox").TNumber;
/**
* UUID v4 format type
* @param {Object} config - Configuration object for the UUID type
* @param {string} [config.default] - Optional default UUID string
* @returns {import('@sinclair/typebox').TString} A TypeBox schema for UUID v4
*/
readonly UUID: (config?: {
default?: string;
}) => import("@sinclair/typebox").TString;
/**
* Email format type
* @param {Object} config - Configuration object for the Email type
* @param {string} [config.default] - Optional default email address
* @returns {import('@sinclair/typebox').TString} A TypeBox schema for email format
*/
readonly Email: (config?: {
default?: string;
}) => import("@sinclair/typebox").TString;
/**
* Mobile number format type (10 digits)
* @param {Object} config - Configuration object for the Mobile type
* @param {string} [config.default] - Optional default mobile number
1 * @returns {import('@sinclair/typebox').TString} A TypeBox schema for mobile number format
*/
readonly Mobile: (config?: {
default?: string;
}) => import("@sinclair/typebox").TString;
/**
* ObjectId type
* @param {Object} config - Configuration object for the ObjectId type
* @param {string} [config.default] - Optional default value for the ObjectId type
* @returns {import('@sinclair/typebox').TString & { static: ObjectId }} A TypeBox schema for MongoDB ObjectId
*/
readonly ObjectId: (config?: {
default?: string;
}) => TObjectId;
};
/**
* Validates a value against a schema
* @param value The value to validate
* @param schema The schema to validate against (preferably pre-compiled)
* @param containsObjectId Whether the schema contains ObjectId fields
* @param skipOperations Array of operations to skip during validation. default performed operations: ['Clean', 'Default', 'Convert', 'ConvertOID']
* @returns Tuple of [error message or null, validated value]
* @note
* - When nothing is specified then it will perform all operations
* - When `Convert` is specified then it will convert the ObjectId string to ObjectId instance
* - When `Default` is specified then it will set the default value
* - When `Clean` is specified then it will remove the extra spaces and trim the string
* - Validate will never mutate the original value as it creates a clone of the value 1st
* @example
* const [error, value] = validate(data, schema);
* if (error) {
* console.error(error);
* } else {
* // use validated value
* }
*/
declare function Encode<Type extends TSchema, Result = StaticEncode<Type>>(value: unknown, type: Type, applyDefault?: boolean, removeExcessProperties?: boolean): [error: string | null, result: Result];
declare function Decode<Type extends TSchema, Result = StaticDecode<Type>>(value: unknown, type: Type, applyDefault?: boolean, removeExcessProperties?: boolean): [error: string | null, result: Result];
type TObjectId = TSchema & {
static: ObjectId;
};
export { Static, Type, Encode, Decode, Utils, type TObjectId };
export type { TSchema, TObject, TArray, TBoolean, TDate, TFunction, TInteger, TLiteral, TNull, TNumber, TPromise, TRecord, TString, TTuple, TUnion, TUnknown, TVoid } from '@sinclair/typebox';