taro-bluetooth-print 2.9.0 → 2.9.2
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/CHANGELOG.md +16 -1
- package/README.md +53 -4
- package/dist/index.cjs.js +1 -1
- package/dist/index.es.js +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/types/core/di/Container.d.ts +84 -0
- package/dist/types/core/di/Tokens.d.ts +29 -0
- package/dist/types/core/di/index.d.ts +3 -0
- package/dist/types/core/event/EventBus.d.ts +66 -0
- package/dist/types/core/event/index.d.ts +2 -0
- package/dist/types/core/index.d.ts +5 -4
- package/dist/types/core/plugin/PluginManager.d.ts +64 -0
- package/dist/types/core/plugin/index.d.ts +2 -0
- package/dist/types/device/MultiPrinterManager.d.ts +2 -0
- package/dist/types/factory/di-factory.d.ts +52 -0
- package/dist/types/index.d.ts +5 -1
- package/dist/types/providers/ServiceProvider.d.ts +56 -0
- package/dist/types/providers/index.d.ts +2 -0
- package/dist/types/template/TemplateEngine.d.ts +24 -68
- package/dist/types/template/engines/TemplateRenderer.d.ts +71 -0
- package/dist/types/template/parsers/TemplateParser.d.ts +23 -0
- package/dist/types/utils/index.d.ts +8 -0
- package/dist/types/utils/logger.d.ts +4 -3
- package/dist/types/utils/outputLimiter.d.ts +87 -0
- package/dist/types/utils/validation.d.ts +11 -309
- package/dist/types/utils/validators/array.d.ts +19 -0
- package/dist/types/utils/validators/buffer.d.ts +18 -0
- package/dist/types/utils/validators/chain.d.ts +31 -0
- package/dist/types/utils/validators/common.d.ts +22 -0
- package/dist/types/utils/validators/number.d.ts +20 -0
- package/dist/types/utils/validators/object.d.ts +24 -0
- package/dist/types/utils/validators/printer.d.ts +40 -0
- package/dist/types/utils/validators/types.d.ts +125 -0
- package/dist/types/utils/validators/uuid.d.ts +23 -0
- package/package.json +1 -1
- package/src/core/BluetoothPrinter.ts +2 -1
- package/src/core/di/Container.ts +332 -0
- package/src/core/di/Tokens.ts +45 -0
- package/src/core/di/index.ts +3 -0
- package/src/core/event/EventBus.ts +251 -0
- package/src/core/event/index.ts +2 -0
- package/src/core/index.ts +10 -4
- package/src/core/plugin/PluginManager.ts +161 -0
- package/src/core/plugin/index.ts +2 -0
- package/src/device/MultiPrinterManager.ts +15 -6
- package/src/factory/di-factory.ts +61 -0
- package/src/index.ts +50 -1
- package/src/providers/ServiceProvider.ts +213 -0
- package/src/providers/index.ts +2 -0
- package/src/template/TemplateEngine.ts +27 -792
- package/src/template/engines/TemplateRenderer.ts +762 -0
- package/src/template/parsers/TemplateParser.ts +94 -0
- package/src/utils/index.ts +9 -0
- package/src/utils/logger.ts +17 -4
- package/src/utils/outputLimiter.ts +227 -0
- package/src/utils/validation.ts +21 -1138
- package/src/utils/validators/array.ts +95 -0
- package/src/utils/validators/buffer.ts +81 -0
- package/src/utils/validators/chain.ts +181 -0
- package/src/utils/validators/common.ts +216 -0
- package/src/utils/validators/number.ts +101 -0
- package/src/utils/validators/object.ts +63 -0
- package/src/utils/validators/printer.ts +294 -0
- package/src/utils/validators/types.ts +105 -0
- package/src/utils/validators/uuid.ts +49 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 输出限制工具 - 防止响应被截断
|
|
3
|
+
* 用于处理大量数据输出时的截断问题
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* 截断配置选项
|
|
7
|
+
*/
|
|
8
|
+
export interface TruncateOptions {
|
|
9
|
+
/** 最大长度限制 */
|
|
10
|
+
maxLength?: number;
|
|
11
|
+
/** 截断后添加的后缀 */
|
|
12
|
+
suffix?: string;
|
|
13
|
+
/** 是否保留开头部分 */
|
|
14
|
+
preserveHead?: boolean;
|
|
15
|
+
/** 保留开头的字符数(当 preserveHead 为 true 时有效) */
|
|
16
|
+
headLength?: number;
|
|
17
|
+
/** 保留结尾的字符数(当 preserveHead 为 true 时有效) */
|
|
18
|
+
tailLength?: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 智能截断字符串,避免输出过长被截断
|
|
22
|
+
*
|
|
23
|
+
* @param str - 原始字符串
|
|
24
|
+
* @param options - 截断选项
|
|
25
|
+
* @returns 截断后的字符串
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // 简单截断
|
|
30
|
+
* const short = truncateString(longText, { maxLength: 1000 });
|
|
31
|
+
*
|
|
32
|
+
* // 保留首尾
|
|
33
|
+
* const summary = truncateString(longText, {
|
|
34
|
+
* preserveHead: true,
|
|
35
|
+
* headLength: 500,
|
|
36
|
+
* tailLength: 500
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare function truncateString(str: string, options?: TruncateOptions): string;
|
|
41
|
+
/**
|
|
42
|
+
* 截断对象/数组的字符串表示,用于日志输出
|
|
43
|
+
*
|
|
44
|
+
* @param data - 任意数据
|
|
45
|
+
* @param maxLength - 最大长度
|
|
46
|
+
* @returns 截断后的字符串
|
|
47
|
+
*/
|
|
48
|
+
export declare function truncateForLog(data: unknown, maxLength?: number): string;
|
|
49
|
+
/**
|
|
50
|
+
* 分批处理大量数据,避免一次性输出过多
|
|
51
|
+
*
|
|
52
|
+
* @param items - 数据项数组
|
|
53
|
+
* @param batchSize - 每批大小
|
|
54
|
+
* @param processor - 处理函数
|
|
55
|
+
* @returns 处理结果
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const results = await batchProcess(
|
|
60
|
+
* largeArray,
|
|
61
|
+
* 100,
|
|
62
|
+
* async (batch) => {
|
|
63
|
+
* console.log(`Processing batch of ${batch.length} items`);
|
|
64
|
+
* return batch.map(item => process(item));
|
|
65
|
+
* }
|
|
66
|
+
* );
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export declare function batchProcess<T, R>(items: T[], batchSize: number, processor: (batch: T[], batchIndex: number) => Promise<R[]> | R[]): Promise<R[]>;
|
|
70
|
+
/**
|
|
71
|
+
* 创建受限制的日志输出函数
|
|
72
|
+
*
|
|
73
|
+
* @param maxLength - 单条日志最大长度
|
|
74
|
+
* @returns 受限的日志函数
|
|
75
|
+
*/
|
|
76
|
+
export declare function createLimitedLogger(maxLength?: number): {
|
|
77
|
+
log: (...args: unknown[]) => void;
|
|
78
|
+
warn: (...args: unknown[]) => void;
|
|
79
|
+
error: (...args: unknown[]) => void;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* 数据摘要生成器 - 用于生成数据的简短摘要
|
|
83
|
+
*
|
|
84
|
+
* @param data - 任意数据
|
|
85
|
+
* @returns 数据摘要
|
|
86
|
+
*/
|
|
87
|
+
export declare function generateSummary(data: unknown): string;
|
|
@@ -14,312 +14,14 @@
|
|
|
14
14
|
* }
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
export
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
value?: unknown;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Validation result
|
|
32
|
-
*/
|
|
33
|
-
export interface ValidationResult {
|
|
34
|
-
/** Whether validation passed */
|
|
35
|
-
valid: boolean;
|
|
36
|
-
/** List of validation errors */
|
|
37
|
-
errors: ValidationError[];
|
|
38
|
-
/** Warnings (non-fatal issues) */
|
|
39
|
-
warnings?: ValidationError[];
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Validator function type
|
|
43
|
-
*/
|
|
44
|
-
export type ValidatorFn<T = unknown> = (value: T) => ValidationResult;
|
|
45
|
-
/**
|
|
46
|
-
* Validation rule definition
|
|
47
|
-
*/
|
|
48
|
-
export interface ValidationRule<T = unknown> {
|
|
49
|
-
/** Rule name */
|
|
50
|
-
name: string;
|
|
51
|
-
/** Validation function */
|
|
52
|
-
validate: (value: T) => boolean;
|
|
53
|
-
/** Error message if validation fails */
|
|
54
|
-
message: string;
|
|
55
|
-
/** Error code */
|
|
56
|
-
code: string;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Common error codes
|
|
60
|
-
*/
|
|
61
|
-
export declare const ValidationCodes: {
|
|
62
|
-
readonly REQUIRED: "REQUIRED";
|
|
63
|
-
readonly INVALID_TYPE: "INVALID_TYPE";
|
|
64
|
-
readonly INVALID_FORMAT: "INVALID_FORMAT";
|
|
65
|
-
readonly OUT_OF_RANGE: "OUT_OF_RANGE";
|
|
66
|
-
readonly TOO_SHORT: "TOO_SHORT";
|
|
67
|
-
readonly TOO_LONG: "TOO_LONG";
|
|
68
|
-
readonly INVALID_ENUM: "INVALID_ENUM";
|
|
69
|
-
readonly PATTERN_MISMATCH: "PATTERN_MISMATCH";
|
|
70
|
-
readonly INVALID_BUFFER: "INVALID_BUFFER";
|
|
71
|
-
readonly INVALID_ENCODING: "INVALID_ENCODING";
|
|
72
|
-
readonly EMPTY_ARRAY: "EMPTY_ARRAY";
|
|
73
|
-
readonly ARRAY_TOO_SHORT: "ARRAY_TOO_SHORT";
|
|
74
|
-
readonly ARRAY_TOO_LONG: "ARRAY_TOO_LONG";
|
|
75
|
-
readonly DUPLICATE_VALUE: "DUPLICATE_VALUE";
|
|
76
|
-
readonly NEGATIVE_VALUE: "NEGATIVE_VALUE";
|
|
77
|
-
readonly ZERO_VALUE: "ZERO_VALUE";
|
|
78
|
-
readonly NOT_A_NUMBER: "NOT_A_NUMBER";
|
|
79
|
-
readonly INFINITY_VALUE: "INFINITY_VALUE";
|
|
80
|
-
readonly INVALID_JSON: "INVALID_JSON";
|
|
81
|
-
readonly UNSUPPORTED_VALUE: "UNSUPPORTED_VALUE";
|
|
82
|
-
readonly INVALID_VERSION: "INVALID_VERSION";
|
|
83
|
-
};
|
|
84
|
-
/**
|
|
85
|
-
* Common validation rules
|
|
86
|
-
*/
|
|
87
|
-
export declare const CommonValidators: {
|
|
88
|
-
/** Required string (non-empty) */
|
|
89
|
-
requiredString: (field: string, value: unknown) => ValidationError | null;
|
|
90
|
-
/** Positive number */
|
|
91
|
-
positiveNumber: (field: string, value: unknown) => ValidationError | null;
|
|
92
|
-
/** Non-negative number */
|
|
93
|
-
nonNegativeNumber: (field: string, value: unknown) => ValidationError | null;
|
|
94
|
-
/** Array with items */
|
|
95
|
-
nonEmptyArray: (field: string, value: unknown) => ValidationError | null;
|
|
96
|
-
/** In range */
|
|
97
|
-
inRange: (field: string, value: unknown, min: number, max: number) => ValidationError | null;
|
|
98
|
-
/** String length */
|
|
99
|
-
stringLength: (field: string, value: unknown, min: number, max: number) => ValidationError | null;
|
|
100
|
-
/** Matches pattern */
|
|
101
|
-
matchesPattern: (field: string, value: unknown, pattern: RegExp, message?: string) => ValidationError | null;
|
|
102
|
-
/** Is one of enum values */
|
|
103
|
-
isEnum: <T extends string>(field: string, value: unknown, enumValues: readonly T[]) => ValidationError | null;
|
|
104
|
-
};
|
|
105
|
-
/**
|
|
106
|
-
* Printer data validation schema
|
|
107
|
-
*/
|
|
108
|
-
export interface PrinterDataSchema {
|
|
109
|
-
/** Device ID */
|
|
110
|
-
deviceId?: {
|
|
111
|
-
required?: boolean;
|
|
112
|
-
pattern?: RegExp;
|
|
113
|
-
};
|
|
114
|
-
/** Device name */
|
|
115
|
-
deviceName?: {
|
|
116
|
-
required?: boolean;
|
|
117
|
-
maxLength?: number;
|
|
118
|
-
};
|
|
119
|
-
/** Service UUID */
|
|
120
|
-
serviceUUID?: {
|
|
121
|
-
required?: boolean;
|
|
122
|
-
pattern?: RegExp;
|
|
123
|
-
};
|
|
124
|
-
/** Characteristic UUID */
|
|
125
|
-
characteristicUUID?: {
|
|
126
|
-
required?: boolean;
|
|
127
|
-
pattern?: RegExp;
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Print job data validation schema
|
|
132
|
-
*/
|
|
133
|
-
export interface PrintJobSchema {
|
|
134
|
-
/** Job ID */
|
|
135
|
-
jobId?: {
|
|
136
|
-
required?: boolean;
|
|
137
|
-
maxLength?: number;
|
|
138
|
-
};
|
|
139
|
-
/** Data buffer */
|
|
140
|
-
data?: {
|
|
141
|
-
required?: boolean;
|
|
142
|
-
minSize?: number;
|
|
143
|
-
maxSize?: number;
|
|
144
|
-
};
|
|
145
|
-
/** Priority */
|
|
146
|
-
priority?: {
|
|
147
|
-
min?: number;
|
|
148
|
-
max?: number;
|
|
149
|
-
};
|
|
150
|
-
/** Retry count */
|
|
151
|
-
retryCount?: {
|
|
152
|
-
min?: number;
|
|
153
|
-
max?: number;
|
|
154
|
-
};
|
|
155
|
-
/** Metadata */
|
|
156
|
-
metadata?: {
|
|
157
|
-
required?: boolean;
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Validate printer connection data
|
|
162
|
-
*
|
|
163
|
-
* @param data - Data to validate
|
|
164
|
-
* @param schema - Validation schema
|
|
165
|
-
* @returns Validation result
|
|
166
|
-
*
|
|
167
|
-
* @example
|
|
168
|
-
* ```typescript
|
|
169
|
-
* const result = validatePrinterData({
|
|
170
|
-
* deviceId: 'device-123',
|
|
171
|
-
* deviceName: 'Thermal Printer',
|
|
172
|
-
* }, {
|
|
173
|
-
* deviceId: { required: true },
|
|
174
|
-
* deviceName: { required: true, maxLength: 50 }
|
|
175
|
-
* });
|
|
176
|
-
* ```
|
|
177
|
-
*/
|
|
178
|
-
export declare function validatePrinterData(data: Record<string, unknown>, schema: PrinterDataSchema): ValidationResult;
|
|
179
|
-
/**
|
|
180
|
-
* Validate print job data
|
|
181
|
-
*
|
|
182
|
-
* @param data - Data to validate
|
|
183
|
-
* @param schema - Validation schema
|
|
184
|
-
* @returns Validation result
|
|
185
|
-
*
|
|
186
|
-
* @example
|
|
187
|
-
* ```typescript
|
|
188
|
-
* const result = validatePrintJob({
|
|
189
|
-
* jobId: 'job-001',
|
|
190
|
-
* data: new ArrayBuffer(1024),
|
|
191
|
-
* priority: 5,
|
|
192
|
-
* }, {
|
|
193
|
-
* jobId: { required: true },
|
|
194
|
-
* data: { required: true, minSize: 1 }
|
|
195
|
-
* });
|
|
196
|
-
* ```
|
|
197
|
-
*/
|
|
198
|
-
export declare function validatePrintJob(data: Record<string, unknown>, schema: PrintJobSchema): ValidationResult;
|
|
199
|
-
/**
|
|
200
|
-
* Validate an ArrayBuffer
|
|
201
|
-
*
|
|
202
|
-
* @param buffer - Buffer to validate
|
|
203
|
-
* @param options - Validation options
|
|
204
|
-
* @returns Validation result
|
|
205
|
-
*
|
|
206
|
-
* @example
|
|
207
|
-
* ```typescript
|
|
208
|
-
* const result = isValidBuffer(data, { minSize: 1, maxSize: 1024 * 1024 });
|
|
209
|
-
* ```
|
|
210
|
-
*/
|
|
211
|
-
export declare function isValidBuffer(buffer: unknown, options?: {
|
|
212
|
-
minSize?: number;
|
|
213
|
-
maxSize?: number;
|
|
214
|
-
required?: boolean;
|
|
215
|
-
}): ValidationResult;
|
|
216
|
-
/**
|
|
217
|
-
* Validate a string is valid UUID
|
|
218
|
-
*
|
|
219
|
-
* @param value - Value to validate
|
|
220
|
-
* @param options - Validation options
|
|
221
|
-
* @returns Validation result
|
|
222
|
-
*
|
|
223
|
-
* @example
|
|
224
|
-
* ```typescript
|
|
225
|
-
* const result = isValidUUID(value, { required: true });
|
|
226
|
-
* ```
|
|
227
|
-
*/
|
|
228
|
-
export declare function isValidUUID(value: unknown, options?: {
|
|
229
|
-
required?: boolean;
|
|
230
|
-
versions?: number[];
|
|
231
|
-
}): {
|
|
232
|
-
valid: boolean;
|
|
233
|
-
version?: number;
|
|
234
|
-
error?: string;
|
|
235
|
-
};
|
|
236
|
-
/**
|
|
237
|
-
* Validate numeric range
|
|
238
|
-
*
|
|
239
|
-
* @param value - Value to validate
|
|
240
|
-
* @param field - Field name for error messages
|
|
241
|
-
* @param options - Range options
|
|
242
|
-
* @returns Validation result
|
|
243
|
-
*
|
|
244
|
-
* @example
|
|
245
|
-
* ```typescript
|
|
246
|
-
* const result = validateRange(value, 'timeout', { min: 0, max: 30000, integer: true });
|
|
247
|
-
* ```
|
|
248
|
-
*/
|
|
249
|
-
export declare function validateRange(value: unknown, field: string, options?: {
|
|
250
|
-
required?: boolean;
|
|
251
|
-
min?: number;
|
|
252
|
-
max?: number;
|
|
253
|
-
integer?: boolean;
|
|
254
|
-
}): ValidationResult;
|
|
255
|
-
/**
|
|
256
|
-
* Validate an object against a schema
|
|
257
|
-
*
|
|
258
|
-
* @param data - Data object to validate
|
|
259
|
-
* @param rules - Validation rules per field
|
|
260
|
-
* @returns Validation result
|
|
261
|
-
*
|
|
262
|
-
* @example
|
|
263
|
-
* ```typescript
|
|
264
|
-
* const result = validateObject(data, {
|
|
265
|
-
* name: [
|
|
266
|
-
* { name: 'required', validate: v => v !== undefined, message: 'Name is required', code: 'REQUIRED' },
|
|
267
|
-
* { name: 'maxLength', validate: v => v.length <= 50, message: 'Max 50 chars', code: 'TOO_LONG' }
|
|
268
|
-
* ],
|
|
269
|
-
* age: [
|
|
270
|
-
* { name: 'number', validate: v => typeof v === 'number', message: 'Must be number', code: 'INVALID_TYPE' }
|
|
271
|
-
* ]
|
|
272
|
-
* });
|
|
273
|
-
* ```
|
|
274
|
-
*/
|
|
275
|
-
export declare function validateObject<T extends Record<string, unknown>>(data: T, rules: {
|
|
276
|
-
[K in keyof T]?: ValidationRule<T[K]>[];
|
|
277
|
-
}): ValidationResult;
|
|
278
|
-
/**
|
|
279
|
-
* Validate an array of items
|
|
280
|
-
*
|
|
281
|
-
* @param items - Array to validate
|
|
282
|
-
* @param itemValidator - Validator function for each item
|
|
283
|
-
* @param options - Array validation options
|
|
284
|
-
* @returns Validation result
|
|
285
|
-
*
|
|
286
|
-
* @example
|
|
287
|
-
* ```typescript
|
|
288
|
-
* const result = validateArray(items, item => validatePrintJob(item, schema), { minItems: 1, maxItems: 100 });
|
|
289
|
-
* ```
|
|
290
|
-
*/
|
|
291
|
-
export declare function validateArray<T>(items: unknown, itemValidator: (item: T, index: number) => ValidationResult, options?: {
|
|
292
|
-
required?: boolean;
|
|
293
|
-
minItems?: number;
|
|
294
|
-
maxItems?: number;
|
|
295
|
-
}): ValidationResult;
|
|
296
|
-
/**
|
|
297
|
-
* Create a chainable validation helper
|
|
298
|
-
*
|
|
299
|
-
* @param value - Value to validate
|
|
300
|
-
* @param field - Field name
|
|
301
|
-
* @returns Chainable validator
|
|
302
|
-
*
|
|
303
|
-
* @example
|
|
304
|
-
* ```typescript
|
|
305
|
-
* const result = check(value, 'timeout')
|
|
306
|
-
* .required()
|
|
307
|
-
* .number()
|
|
308
|
-
* .range(0, 30000)
|
|
309
|
-
* .integer()
|
|
310
|
-
* .result();
|
|
311
|
-
* ```
|
|
312
|
-
*/
|
|
313
|
-
export interface ChainableValidator {
|
|
314
|
-
addError(message: string, code: string): ChainableValidator;
|
|
315
|
-
required(): ChainableValidator;
|
|
316
|
-
string(): ChainableValidator;
|
|
317
|
-
number(): ChainableValidator;
|
|
318
|
-
integer(): ChainableValidator;
|
|
319
|
-
range(min: number, max: number): ChainableValidator;
|
|
320
|
-
length(min: number, max: number): ChainableValidator;
|
|
321
|
-
pattern(regex: RegExp, message?: string): ChainableValidator;
|
|
322
|
-
oneOf<T>(values: readonly T[], message?: string): ChainableValidator;
|
|
323
|
-
result(): ValidationResult;
|
|
324
|
-
}
|
|
325
|
-
export declare function check(value: unknown, field: string): ChainableValidator;
|
|
17
|
+
export { validatePrinterData, validatePrintJob } from './validators/printer';
|
|
18
|
+
export { isValidBuffer } from './validators/buffer';
|
|
19
|
+
export { isValidUUID } from './validators/uuid';
|
|
20
|
+
export { validateRange } from './validators/number';
|
|
21
|
+
export { validateObject } from './validators/object';
|
|
22
|
+
export { validateArray } from './validators/array';
|
|
23
|
+
export { check } from './validators/chain';
|
|
24
|
+
export type { ChainableValidator } from './validators/chain';
|
|
25
|
+
export { CommonValidators } from './validators/common';
|
|
26
|
+
export { ValidationCodes } from './validators/types';
|
|
27
|
+
export type { ValidationError, ValidationResult, ValidatorFn, ValidationRule, PrinterDataSchema, PrintJobSchema, } from './validators/types';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ValidationResult } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Validate an array of items
|
|
4
|
+
*
|
|
5
|
+
* @param items - Array to validate
|
|
6
|
+
* @param itemValidator - Validator function for each item
|
|
7
|
+
* @param options - Array validation options
|
|
8
|
+
* @returns Validation result
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const result = validateArray(items, item => validatePrintJob(item, schema), { minItems: 1, maxItems: 100 });
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function validateArray<T>(items: unknown, itemValidator: (item: T, index: number) => ValidationResult, options?: {
|
|
16
|
+
required?: boolean;
|
|
17
|
+
minItems?: number;
|
|
18
|
+
maxItems?: number;
|
|
19
|
+
}): ValidationResult;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ValidationResult } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Validate an ArrayBuffer
|
|
4
|
+
*
|
|
5
|
+
* @param buffer - Buffer to validate
|
|
6
|
+
* @param options - Validation options
|
|
7
|
+
* @returns Validation result
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const result = isValidBuffer(data, { minSize: 1, maxSize: 1024 * 1024 });
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare function isValidBuffer(buffer: unknown, options?: {
|
|
15
|
+
minSize?: number;
|
|
16
|
+
maxSize?: number;
|
|
17
|
+
required?: boolean;
|
|
18
|
+
}): ValidationResult;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ValidationResult } from './types';
|
|
2
|
+
export interface ChainableValidator {
|
|
3
|
+
addError(message: string, code: string): ChainableValidator;
|
|
4
|
+
required(): ChainableValidator;
|
|
5
|
+
string(): ChainableValidator;
|
|
6
|
+
number(): ChainableValidator;
|
|
7
|
+
integer(): ChainableValidator;
|
|
8
|
+
range(min: number, max: number): ChainableValidator;
|
|
9
|
+
length(min: number, max: number): ChainableValidator;
|
|
10
|
+
pattern(regex: RegExp, message?: string): ChainableValidator;
|
|
11
|
+
oneOf<T>(values: readonly T[], message?: string): ChainableValidator;
|
|
12
|
+
result(): ValidationResult;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Create a chainable validation helper
|
|
16
|
+
*
|
|
17
|
+
* @param value - Value to validate
|
|
18
|
+
* @param field - Field name
|
|
19
|
+
* @returns Chainable validator
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const result = check(value, 'timeout')
|
|
24
|
+
* .required()
|
|
25
|
+
* .number()
|
|
26
|
+
* .range(0, 30000)
|
|
27
|
+
* .integer()
|
|
28
|
+
* .result();
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function check(value: unknown, field: string): ChainableValidator;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ValidationError } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Common validation rules
|
|
4
|
+
*/
|
|
5
|
+
export declare const CommonValidators: {
|
|
6
|
+
/** Required string (non-empty) */
|
|
7
|
+
requiredString: (field: string, value: unknown) => ValidationError | null;
|
|
8
|
+
/** Positive number */
|
|
9
|
+
positiveNumber: (field: string, value: unknown) => ValidationError | null;
|
|
10
|
+
/** Non-negative number */
|
|
11
|
+
nonNegativeNumber: (field: string, value: unknown) => ValidationError | null;
|
|
12
|
+
/** Array with items */
|
|
13
|
+
nonEmptyArray: (field: string, value: unknown) => ValidationError | null;
|
|
14
|
+
/** In range */
|
|
15
|
+
inRange: (field: string, value: unknown, min: number, max: number) => ValidationError | null;
|
|
16
|
+
/** String length */
|
|
17
|
+
stringLength: (field: string, value: unknown, min: number, max: number) => ValidationError | null;
|
|
18
|
+
/** Matches pattern */
|
|
19
|
+
matchesPattern: (field: string, value: unknown, pattern: RegExp, message?: string) => ValidationError | null;
|
|
20
|
+
/** Is one of enum values */
|
|
21
|
+
isEnum: <T extends string>(field: string, value: unknown, enumValues: readonly T[]) => ValidationError | null;
|
|
22
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ValidationResult } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Validate numeric range
|
|
4
|
+
*
|
|
5
|
+
* @param value - Value to validate
|
|
6
|
+
* @param field - Field name for error messages
|
|
7
|
+
* @param options - Range options
|
|
8
|
+
* @returns Validation result
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const result = validateRange(value, 'timeout', { min: 0, max: 30000, integer: true });
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function validateRange(value: unknown, field: string, options?: {
|
|
16
|
+
required?: boolean;
|
|
17
|
+
min?: number;
|
|
18
|
+
max?: number;
|
|
19
|
+
integer?: boolean;
|
|
20
|
+
}): ValidationResult;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ValidationResult, ValidationRule } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Validate an object against a schema
|
|
4
|
+
*
|
|
5
|
+
* @param data - Data object to validate
|
|
6
|
+
* @param rules - Validation rules per field
|
|
7
|
+
* @returns Validation result
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const result = validateObject(data, {
|
|
12
|
+
* name: [
|
|
13
|
+
* { name: 'required', validate: v => v !== undefined, message: 'Name is required', code: 'REQUIRED' },
|
|
14
|
+
* { name: 'maxLength', validate: v => v.length <= 50, message: 'Max 50 chars', code: 'TOO_LONG' }
|
|
15
|
+
* ],
|
|
16
|
+
* age: [
|
|
17
|
+
* { name: 'number', validate: v => typeof v === 'number', message: 'Must be number', code: 'INVALID_TYPE' }
|
|
18
|
+
* ]
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function validateObject<T extends Record<string, unknown>>(data: T, rules: {
|
|
23
|
+
[K in keyof T]?: ValidationRule<T[K]>[];
|
|
24
|
+
}): ValidationResult;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ValidationResult, PrinterDataSchema, PrintJobSchema } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Validate printer connection data
|
|
4
|
+
*
|
|
5
|
+
* @param data - Data to validate
|
|
6
|
+
* @param schema - Validation schema
|
|
7
|
+
* @returns Validation result
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const result = validatePrinterData({
|
|
12
|
+
* deviceId: 'device-123',
|
|
13
|
+
* deviceName: 'Thermal Printer',
|
|
14
|
+
* }, {
|
|
15
|
+
* deviceId: { required: true },
|
|
16
|
+
* deviceName: { required: true, maxLength: 50 }
|
|
17
|
+
* });
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare function validatePrinterData(data: Record<string, unknown>, schema: PrinterDataSchema): ValidationResult;
|
|
21
|
+
/**
|
|
22
|
+
* Validate print job data
|
|
23
|
+
*
|
|
24
|
+
* @param data - Data to validate
|
|
25
|
+
* @param schema - Validation schema
|
|
26
|
+
* @returns Validation result
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const result = validatePrintJob({
|
|
31
|
+
* jobId: 'job-001',
|
|
32
|
+
* data: new ArrayBuffer(1024),
|
|
33
|
+
* priority: 5,
|
|
34
|
+
* }, {
|
|
35
|
+
* jobId: { required: true },
|
|
36
|
+
* data: { required: true, minSize: 1 }
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare function validatePrintJob(data: Record<string, unknown>, schema: PrintJobSchema): ValidationResult;
|