@nhtio/validation 0.1.0-master-5ddd6b01

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@nhtio/validation",
3
+ "version": "0.1.0-master-5ddd6b01",
4
+ "description": "A powerful schema description language and data validator",
5
+ "keywords": [],
6
+ "author": "Jak Giveon <jak@nht.io>",
7
+ "copyright": "© 2025-present New Horizon Technology LTD",
8
+ "license": "MIT",
9
+ "module": "./index.mjs",
10
+ "main": "./index.cjs",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./index.mjs",
14
+ "require": "./index.cjs",
15
+ "types": "./index.d.ts"
16
+ }
17
+ },
18
+ "pnpm": {
19
+ "overrides": {
20
+ "vite": "npm:rolldown-vite@latest"
21
+ },
22
+ "onlyBuiltDependencies": [
23
+ "es5-ext"
24
+ ]
25
+ },
26
+ "packageManager": "pnpm@10.8.0+sha512.0e82714d1b5b43c74610193cb20734897c1d00de89d0e18420aebc5977fa13d780a9cb05734624e81ebd81cc876cd464794850641c48b9544326b5622ca29971",
27
+ "type": "module",
28
+ "dependencies": {}
29
+ }
@@ -0,0 +1,67 @@
1
+ import { DateTime } from 'luxon';
2
+ import type { DateObjectUnits } from 'luxon';
3
+ /**
4
+ * Checks if a value is an instance of a specific class.
5
+ * @param value - The value to check
6
+ * @param type - The name of the class to check against
7
+ * @returns true if the value is an instance of the specified class, false otherwise
8
+ */
9
+ export declare const isInstanceOf: <T>(value: unknown, type: string, ctor?: new (...args: any[]) => T) => value is T;
10
+ /**
11
+ * Type guard to check if a value is a Luxon DateTime instance
12
+ * @param value - The value to check
13
+ * @returns True if the value is a Luxon DateTime, false otherwise
14
+ */
15
+ export declare const isLuxonDateTime: (value: unknown) => value is DateTime;
16
+ /**
17
+ * Type guard to check if a value is a plain object (not null, not array)
18
+ * @param value - The value to check
19
+ * @returns True if the value is a plain object, false otherwise
20
+ */
21
+ export declare const isPlainObject: (value: unknown) => value is {
22
+ [key: string]: unknown;
23
+ };
24
+ /**
25
+ * Type guard to check if a value is an array
26
+ * @param value - The value to check
27
+ * @returns True if the value is an array, false otherwise
28
+ */
29
+ export declare const isArray: (value: unknown) => value is unknown[];
30
+ /**
31
+ * Type guard to check if a value is a valid Luxon DateObjectUnits object.
32
+ *
33
+ * This function validates that the input is a plain object containing only valid
34
+ * Luxon date/time unit properties with numeric values. It ensures type safety
35
+ * when working with objects that should represent date/time units for Luxon operations.
36
+ *
37
+ * @param value - The value to check for DateObjectUnits compatibility
38
+ * @returns True if the value is a valid DateObjectUnits object, false otherwise
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * // Valid DateObjectUnits objects
43
+ * const validUnits1 = { year: 2023, month: 12, day: 25 };
44
+ * const validUnits2 = { hour: 14, minute: 30, second: 45 };
45
+ * const validUnits3 = { weekYear: 2023, weekNumber: 52 };
46
+ *
47
+ * console.log(isDateObjectUnits(validUnits1)); // true
48
+ * console.log(isDateObjectUnits(validUnits2)); // true
49
+ * console.log(isDateObjectUnits(validUnits3)); // true
50
+ *
51
+ * // Invalid cases
52
+ * console.log(isDateObjectUnits({})); // false (empty object)
53
+ * console.log(isDateObjectUnits({ year: "2023" })); // false (string value)
54
+ * console.log(isDateObjectUnits({ invalid: 123 })); // false (invalid property)
55
+ * console.log(isDateObjectUnits(null)); // false
56
+ * console.log(isDateObjectUnits([])); // false
57
+ * ```
58
+ *
59
+ * @remarks
60
+ * The function validates against all supported Luxon DateObjectUnits properties:
61
+ * - Time units: year, month, day, ordinal
62
+ * - Week-based units: weekYear, localWeekYear, weekNumber, localWeekNumber, weekday, localWeekday
63
+ * - Time of day units: hour, minute, second, millisecond
64
+ *
65
+ * All property values must be numbers or undefined. The object must contain at least one property.
66
+ */
67
+ export declare const isDateObjectUnits: (value: unknown) => value is DateObjectUnits;
@@ -0,0 +1,51 @@
1
+ import type { Root } from 'joi';
2
+ import type { BigIntSchema } from './schemas/bigint';
3
+ import type { DatetimeSchema } from './schemas/datetime';
4
+ /**
5
+ * Extended Joi root interface that includes custom schema types for
6
+ * additional validation scenarios.
7
+ *
8
+ * This interface extends the standard Joi Root interface to include
9
+ * additional schema types
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * import { validator } from '@nhtio/validation'
14
+ *
15
+ * const schema = validator.object({
16
+ * id: validator.bigint().positive().required(),
17
+ * balance: validator.bigint().min(0n).optional()
18
+ * })
19
+ * ```
20
+ *
21
+ * @public
22
+ */
23
+ export interface ValidationRoot extends Root {
24
+ bigint(): BigIntSchema;
25
+ datetime(): DatetimeSchema;
26
+ }
27
+ /**
28
+ * Extended Joi instance with custom schema types.
29
+ *
30
+ * This instance includes all standard Joi functionality plus additional
31
+ * schema types for additional validation scenarios.
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * import { validator } from '@nhtio/validation'
36
+ *
37
+ * // Standard Joi usage
38
+ * const userSchema = validator.object({
39
+ * name: validator.string().required(),
40
+ * age: validator.number().min(0),
41
+ * balance: validator.bigint().positive() // Custom BigInt type
42
+ * })
43
+ * ```
44
+ *
45
+ * @public
46
+ */
47
+ export declare const validator: ValidationRoot;
48
+ export type { BigIntSchema };
49
+ export type { DatetimeSchema };
50
+ export type * from './schemas';
51
+ export { encode, decode } from './utils';
@@ -0,0 +1,85 @@
1
+ import type { AnySchema } from '../schemas';
2
+ import type { ExtensionFactory, Reference } from 'joi';
3
+ /**
4
+ * A Joi extension that adds support for BigInt validation with comprehensive
5
+ * comparison operations and type coercion.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { validator } from '@nhtio/validation'
10
+ *
11
+ * const schema = validator.bigint()
12
+ * .min(0n)
13
+ * .max(1000n)
14
+ * .required()
15
+ *
16
+ * // Validates and converts to BigInt
17
+ * const result = schema.validate("123") // Returns { value: 123n }
18
+ * ```
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * // Works with all standard Joi methods
23
+ * const optionalSchema = validator.bigint()
24
+ * .positive()
25
+ * .optional()
26
+ * .allow(null)
27
+ * .default(0n)
28
+ * ```
29
+ *
30
+ * @public
31
+ */
32
+ export declare const bigint: ExtensionFactory;
33
+ /**
34
+ * Schema type for BigInt validation with comprehensive comparison operations.
35
+ *
36
+ * This interface extends the base Joi AnySchema to provide BigInt-specific
37
+ * validation methods including range checks, sign validation, and multiple checks.
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * import { validator } from '@nhtio/validation'
42
+ *
43
+ * const schema: BigIntSchema = validator.bigint()
44
+ * .min(0n)
45
+ * .max(1000n)
46
+ * .positive()
47
+ * ```
48
+ *
49
+ * @public
50
+ */
51
+ export interface BigIntSchema<TSchema = bigint> extends AnySchema<TSchema> {
52
+ /**
53
+ * Validates that the BigInt is greater than the specified threshold
54
+ * @param limit - The threshold value to compare against
55
+ */
56
+ greater(limit: bigint | Reference): this;
57
+ /**
58
+ * Validates that the BigInt is less than the specified threshold
59
+ * @param limit - The threshold value to compare against
60
+ */
61
+ less(limit: bigint | Reference): this;
62
+ /**
63
+ * Validates that the BigInt is less than or equal to the specified maximum
64
+ * @param limit - The maximum allowed value
65
+ */
66
+ max(limit: bigint | Reference): this;
67
+ /**
68
+ * Validates that the BigInt is greater than or equal to the specified minimum
69
+ * @param limit - The minimum allowed value
70
+ */
71
+ min(limit: bigint | Reference): this;
72
+ /**
73
+ * Validates that the BigInt is a multiple of the specified factor
74
+ * @param limit - The factor that the value must be a multiple of
75
+ */
76
+ multiple(limit: bigint | Reference): this;
77
+ /**
78
+ * Validates that the BigInt is negative (less than zero)
79
+ */
80
+ negative(): this;
81
+ /**
82
+ * Validates that the BigInt is positive (greater than zero)
83
+ */
84
+ positive(): this;
85
+ }