starlight-server 1.2.0 → 1.5.0

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.
Files changed (85) hide show
  1. package/README.md +11 -1
  2. package/dist/demo/index.js +41 -27
  3. package/dist/http/cors.d.ts +33 -0
  4. package/dist/http/cors.js +33 -0
  5. package/dist/http/index.d.ts +1 -0
  6. package/dist/http/index.js +1 -0
  7. package/dist/http/mime-types.js +1 -20
  8. package/dist/http/request.d.ts +18 -2
  9. package/dist/http/request.js +28 -7
  10. package/dist/http/response.d.ts +9 -5
  11. package/dist/http/response.js +16 -8
  12. package/dist/http/server.d.ts +1 -1
  13. package/dist/http/server.js +1 -1
  14. package/dist/index.d.ts +3 -1
  15. package/dist/index.js +3 -2
  16. package/dist/logging.d.ts +4 -3
  17. package/dist/logging.js +7 -19
  18. package/dist/router/helpers.d.ts +6 -0
  19. package/dist/router/helpers.js +24 -0
  20. package/dist/router/index.d.ts +67 -38
  21. package/dist/router/index.js +107 -36
  22. package/dist/router/match-path.d.ts +13 -0
  23. package/dist/router/match-path.js +160 -0
  24. package/dist/swagger/factories.d.ts +143 -0
  25. package/dist/swagger/factories.js +231 -0
  26. package/dist/swagger/index.d.ts +54 -6
  27. package/dist/swagger/index.js +94 -58
  28. package/dist/swagger/specification.d.ts +802 -0
  29. package/dist/swagger/specification.js +130 -0
  30. package/package.json +10 -10
  31. package/src/demo/index.ts +45 -42
  32. package/src/http/cors.ts +59 -0
  33. package/src/http/index.ts +1 -0
  34. package/src/http/mime-types.ts +1 -20
  35. package/src/http/request.ts +38 -10
  36. package/src/http/response.ts +18 -17
  37. package/src/http/server.ts +2 -2
  38. package/src/index.ts +3 -2
  39. package/src/logging.ts +7 -21
  40. package/src/router/helpers.ts +27 -0
  41. package/src/router/index.ts +141 -38
  42. package/src/router/match-path.ts +178 -0
  43. package/src/swagger/factories.ts +345 -0
  44. package/src/swagger/index.ts +113 -80
  45. package/src/swagger/specification.ts +823 -0
  46. package/dist/router/cors.d.ts +0 -24
  47. package/dist/router/cors.js +0 -35
  48. package/dist/router/match.d.ts +0 -23
  49. package/dist/router/match.js +0 -172
  50. package/dist/router/parameters.d.ts +0 -50
  51. package/dist/router/parameters.js +0 -118
  52. package/dist/router/router.d.ts +0 -128
  53. package/dist/router/router.js +0 -97
  54. package/dist/swagger/factory.d.ts +0 -97
  55. package/dist/swagger/factory.js +0 -144
  56. package/dist/swagger/openapi-spec.d.ts +0 -261
  57. package/dist/swagger/openapi-spec.js +0 -5
  58. package/dist/validators/array.d.ts +0 -9
  59. package/dist/validators/array.js +0 -28
  60. package/dist/validators/boolean.d.ts +0 -4
  61. package/dist/validators/boolean.js +0 -28
  62. package/dist/validators/common.d.ts +0 -23
  63. package/dist/validators/common.js +0 -25
  64. package/dist/validators/index.d.ts +0 -20
  65. package/dist/validators/index.js +0 -38
  66. package/dist/validators/number.d.ts +0 -10
  67. package/dist/validators/number.js +0 -30
  68. package/dist/validators/object.d.ts +0 -13
  69. package/dist/validators/object.js +0 -36
  70. package/dist/validators/string.d.ts +0 -11
  71. package/dist/validators/string.js +0 -29
  72. package/src/.DS_Store +0 -0
  73. package/src/router/cors.ts +0 -54
  74. package/src/router/match.ts +0 -194
  75. package/src/router/parameters.ts +0 -175
  76. package/src/router/router.ts +0 -234
  77. package/src/swagger/factory.ts +0 -169
  78. package/src/swagger/openapi-spec.ts +0 -312
  79. package/src/validators/array.ts +0 -33
  80. package/src/validators/boolean.ts +0 -23
  81. package/src/validators/common.ts +0 -46
  82. package/src/validators/index.ts +0 -50
  83. package/src/validators/number.ts +0 -36
  84. package/src/validators/object.ts +0 -41
  85. package/src/validators/string.ts +0 -38
@@ -1,97 +0,0 @@
1
- /**
2
- * 把路由定义转换成 swagger API 定义的工具函数
3
- */
4
- import type { RouteDefinition, ResponseDataType, BasicParameter } from '../router/index.js';
5
- import type { Schema, Reference, PathItem } from './openapi-spec.js';
6
- type AnyObject = {
7
- [k: string]: unknown;
8
- };
9
- /**
10
- * 基本数据类型
11
- */
12
- export declare function string(description?: string, options?: AnyObject): {
13
- type: string;
14
- description: string | undefined;
15
- };
16
- export declare function number(description?: string, options?: AnyObject): {
17
- type: string;
18
- description: string | undefined;
19
- };
20
- export declare function boolean(description?: string, options?: AnyObject): {
21
- type: string;
22
- description: string | undefined;
23
- };
24
- export declare function object(properties: Record<string, Schema>, extra?: AnyObject): {
25
- type: "object";
26
- properties: Record<string, Schema>;
27
- };
28
- export declare function array(items: Schema, extra?: AnyObject): {
29
- type: "array";
30
- items: Schema;
31
- };
32
- /**
33
- * 对 components/schemas 中定义的数据类型的引用
34
- */
35
- export declare function ref(name: string, summary?: string, description?: string): Reference;
36
- /**
37
- * 可放置于 components/schemas 中的数据类型定义(用于响应内容)
38
- */
39
- export declare function responseSchema(type: ResponseDataType | ResponseDataType[]): Schema | Reference;
40
- /**
41
- * 可放置于 components/schemas 中的数据类型定义(用于请求内容)
42
- */
43
- export declare function parameterSchema(parameter: BasicParameter): Schema;
44
- /**
45
- * 生成 JSON 形式的 content
46
- */
47
- export declare function jsonMedia(schema: string | Schema): {
48
- content: {
49
- 'application/json': {
50
- schema: Schema | Reference;
51
- };
52
- };
53
- };
54
- /**
55
- * 把一系列路由转换成 swagger API 定义
56
- */
57
- export declare function paths(routes: RouteDefinition[]): Record<string, PathItem>;
58
- /**
59
- * 单个路由定义
60
- */
61
- export declare function operation(route: RouteDefinition): {
62
- tags: string[];
63
- summary: string;
64
- operationId: string;
65
- requestBody: {
66
- content: {
67
- 'application/json': {
68
- schema: Schema | Reference;
69
- };
70
- };
71
- } | undefined;
72
- responses: {
73
- default: {
74
- content: {
75
- 'application/json': {
76
- schema: Schema | Reference;
77
- };
78
- };
79
- description: string;
80
- };
81
- };
82
- };
83
- /**
84
- * 生成路由的唯一 ID
85
- */
86
- export declare function operationId(route: RouteDefinition): string;
87
- /**
88
- * 路由请求参数转为 swagger 定义
89
- */
90
- export declare function requestBody(route: RouteDefinition): {
91
- content: {
92
- 'application/json': {
93
- schema: Schema | Reference;
94
- };
95
- };
96
- } | undefined;
97
- export {};
@@ -1,144 +0,0 @@
1
- /**
2
- * 基本数据类型
3
- */
4
- export function string(description, options) {
5
- return { type: 'string', description, ...(options ?? {}) };
6
- }
7
- export function number(description, options) {
8
- return { type: 'number', description, ...(options ?? {}) };
9
- }
10
- export function boolean(description, options) {
11
- return { type: 'boolean', description, ...(options ?? {}) };
12
- }
13
- export function object(properties, extra) {
14
- return { type: 'object', properties, ...(extra ?? {}) };
15
- }
16
- export function array(items, extra) {
17
- return { type: 'array', items, ...(extra ?? {}) };
18
- }
19
- /**
20
- * 对 components/schemas 中定义的数据类型的引用
21
- */
22
- export function ref(name, summary, description) {
23
- return { $ref: '#/components/schemas/' + name, summary, description };
24
- }
25
- /**
26
- * 可放置于 components/schemas 中的数据类型定义(用于响应内容)
27
- */
28
- export function responseSchema(type) {
29
- if (Array.isArray(type)) {
30
- return {
31
- oneOf: type
32
- .filter((subType) => subType !== 'null')
33
- .map(responseSchema),
34
- nullable: type.includes('null'),
35
- };
36
- }
37
- if (typeof type === 'string')
38
- return { type };
39
- if ('ref' in type)
40
- return ref(type.ref, type.summary, type.description);
41
- if ('array' in type)
42
- return array(responseSchema(type.array));
43
- if ('object' in type) {
44
- return object(type.object.reduce((properties, property) => ({
45
- ...properties,
46
- [property.name]: { ...responseSchema(property.type), description: property.description },
47
- }), {}));
48
- }
49
- return {};
50
- }
51
- /**
52
- * 可放置于 components/schemas 中的数据类型定义(用于请求内容)
53
- */
54
- export function parameterSchema(parameter) {
55
- const schema = {
56
- description: parameter.description,
57
- nullable: parameter.nullable,
58
- default: parameter.defaults,
59
- };
60
- if (typeof parameter.type === 'string') {
61
- schema.type = parameter.type;
62
- }
63
- else if ('array' in parameter.type) {
64
- const arraySchema = array(parameterSchema(parameter.type.array));
65
- Object.assign(schema, arraySchema);
66
- }
67
- else if ('record' in parameter.type) {
68
- const innerSchema = parameterSchema(parameter.type.record);
69
- Object.assign(schema, object({}, { additionalProperties: innerSchema }));
70
- }
71
- else if ('object' in parameter.type) {
72
- const objectSchema = object(Object.entries(parameter.type.object).reduce((properties, [name, subParameter]) => ({
73
- ...properties,
74
- [name]: parameterSchema(subParameter),
75
- }), {}));
76
- Object.assign(schema, objectSchema);
77
- }
78
- return schema;
79
- }
80
- /**
81
- * 生成 JSON 形式的 content
82
- */
83
- export function jsonMedia(schema) {
84
- return {
85
- content: {
86
- 'application/json': {
87
- schema: typeof schema === 'string' ? ref(schema) : schema,
88
- },
89
- },
90
- };
91
- }
92
- /**
93
- * 把一系列路由转换成 swagger API 定义
94
- */
95
- export function paths(routes) {
96
- const paths = {};
97
- for (const route of routes) {
98
- if (route.category === 'swagger')
99
- continue;
100
- if (!paths[route.path])
101
- paths[route.path] = {};
102
- const pathItem = paths[route.path];
103
- const method = route.method.toLowerCase();
104
- pathItem[method] = operation(route);
105
- }
106
- return paths;
107
- }
108
- /**
109
- * 单个路由定义
110
- */
111
- export function operation(route) {
112
- return {
113
- tags: route.category ? [route.category] : [],
114
- summary: route.description,
115
- operationId: operationId(route),
116
- requestBody: requestBody(route),
117
- responses: {
118
- default: {
119
- description: 'OK',
120
- ...jsonMedia(route.response !== undefined ? responseSchema(route.response) : object({})),
121
- },
122
- },
123
- };
124
- }
125
- /**
126
- * 生成路由的唯一 ID
127
- */
128
- export function operationId(route) {
129
- return `${route.method}-${route.path}`;
130
- }
131
- /**
132
- * 路由请求参数转为 swagger 定义
133
- */
134
- export function requestBody(route) {
135
- if (!route.body)
136
- return;
137
- return jsonMedia({
138
- type: 'object',
139
- properties: route.body.reduce((properties, parameter) => ({
140
- ...properties,
141
- [parameter.name]: parameterSchema(parameter),
142
- }), {}),
143
- });
144
- }
@@ -1,261 +0,0 @@
1
- /**
2
- * OpenAPI Specification
3
- * https://swagger.io/specification/
4
- */
5
- export interface OpenAPI {
6
- openapi: '3.1.0';
7
- info: Info;
8
- servers?: Server[];
9
- paths: Paths;
10
- components?: Components;
11
- security?: SecurityRequirement[];
12
- tags?: Tag[];
13
- externalDocs?: ExternalDocumentation;
14
- }
15
- export interface Info extends Extendable {
16
- title: string;
17
- summary?: string;
18
- description?: string;
19
- termsOfService?: string;
20
- contact?: Contact;
21
- license?: License;
22
- version: string;
23
- }
24
- export interface Contact extends Extendable {
25
- name?: string;
26
- url?: string;
27
- email?: string;
28
- }
29
- export interface License extends Extendable {
30
- name: string;
31
- url?: string;
32
- }
33
- export interface Server extends Extendable {
34
- url: string;
35
- description?: CommonMark;
36
- variables?: Record<string, ServerVariable>;
37
- }
38
- export interface ServerVariable extends Extendable {
39
- enum?: [string] & string[];
40
- default: string;
41
- description: CommonMark;
42
- }
43
- export interface Components extends Extendable {
44
- schemas?: Record<string, Schema>;
45
- responses?: Record<string, Response | Reference>;
46
- parameters?: Record<string, Parameter | Reference>;
47
- examples?: Record<string, Example | Reference>;
48
- requestBodies?: Record<string, RequestBody | Reference>;
49
- headers?: Record<string, Header | Reference>;
50
- securitySchemes?: Record<string, SecurityScheme | Reference>;
51
- links?: Record<string, Link | Reference>;
52
- callbacks?: Record<string, Callback | Reference>;
53
- pathItems?: Record<string, PathItem | Reference>;
54
- }
55
- export interface Paths extends Extendable {
56
- [path: string]: PathItem;
57
- }
58
- export interface PathItem extends Extendable {
59
- $ref?: string;
60
- summary?: string;
61
- description?: CommonMark;
62
- get?: Operation;
63
- put?: Operation;
64
- post?: Operation;
65
- delete?: Operation;
66
- options?: Operation;
67
- head?: Operation;
68
- patch?: Operation;
69
- trace?: Operation;
70
- servers?: Server[];
71
- parameters?: (Parameter | Reference)[];
72
- }
73
- export interface Operation {
74
- tags?: string[];
75
- summary?: string;
76
- description?: CommonMark;
77
- externalDocs?: ExternalDocumentation;
78
- operationId?: string;
79
- parameters?: (Parameter | Reference)[];
80
- requestBody?: RequestBody | Reference;
81
- responses: Responses;
82
- callbacks?: Record<string, Callback | Reference>;
83
- deprecated?: boolean;
84
- }
85
- export interface ExternalDocumentation {
86
- description?: CommonMark;
87
- url: string;
88
- }
89
- export interface Parameter extends Extendable {
90
- name: string;
91
- in: ParameterLocation;
92
- description?: CommonMark;
93
- required?: boolean;
94
- deprecated?: boolean;
95
- allowEmptyValue?: boolean;
96
- style?: ParameterStyle;
97
- explode?: boolean;
98
- allowReserved?: boolean;
99
- schema?: Schema | Reference;
100
- example?: unknown;
101
- examples?: Record<string, Example | Reference>;
102
- content?: Record<string, MediaType>;
103
- }
104
- export type ParameterLocation = 'query' | 'header' | 'path' | 'cookie';
105
- export type ParameterStyle = string;
106
- export interface RequestBody extends Extendable {
107
- description?: CommonMark;
108
- content: Record<string, MediaType>;
109
- required?: boolean;
110
- }
111
- export interface MediaType extends Extendable {
112
- schema?: Schema | Reference;
113
- example?: unknown;
114
- examples?: Record<string, Example | Reference>;
115
- encoding?: Record<string, Encoding>;
116
- }
117
- export interface Encoding extends Extendable {
118
- contentType?: string;
119
- headers?: Record<string, Header | Reference>;
120
- style?: ParameterStyle;
121
- explode?: boolean;
122
- allowReserved?: boolean;
123
- }
124
- export interface Responses extends Extendable {
125
- default: Response | Reference;
126
- [HTTPStatus: string]: Response | Reference;
127
- }
128
- export interface Response extends Extendable {
129
- description: CommonMark;
130
- headers?: Record<string, Header | Reference>;
131
- content?: Record<string, MediaType>;
132
- links?: Record<string, Link | Reference>;
133
- }
134
- export interface Callback extends Extendable {
135
- [expression: Expression]: PathItem;
136
- }
137
- export interface Example extends Extendable {
138
- summary?: string;
139
- description?: CommonMark;
140
- value?: unknown;
141
- externalValue?: string;
142
- }
143
- export interface Link extends Extendable {
144
- operationRef?: string;
145
- operationId?: string;
146
- parameters?: Record<string, unknown>;
147
- requestBody?: unknown;
148
- description?: string;
149
- server?: Server;
150
- }
151
- export type Header = Omit<Parameter, 'name' | 'in'>;
152
- export interface Tag extends Extendable {
153
- name: string;
154
- description?: CommonMark;
155
- externalDocs?: ExternalDocumentation;
156
- }
157
- export interface Reference {
158
- $ref: string;
159
- summary?: string;
160
- description?: string;
161
- }
162
- export interface Schema extends Extendable {
163
- nullable?: boolean;
164
- discriminator?: Discriminator;
165
- readonly?: boolean;
166
- writeOnly?: boolean;
167
- xml?: XML;
168
- externalDocs?: ExternalDocumentation;
169
- example?: unknown;
170
- deprecated?: boolean;
171
- multipleOf?: number;
172
- maximum?: number;
173
- exclusiveMaximum?: boolean;
174
- minimum?: number;
175
- exclusiveMinimum?: boolean;
176
- maxLength?: number;
177
- minLength?: number;
178
- pattern?: string;
179
- items?: Schema;
180
- maxItems?: number;
181
- minItems?: number;
182
- uniqueItems?: boolean;
183
- maxProperties?: number;
184
- minProperties?: string;
185
- required?: [string] & string[];
186
- properties?: Record<string, Schema>;
187
- additionalProperties?: boolean | Schema;
188
- enum?: unknown[];
189
- type?: 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'object' | 'null';
190
- allOf?: Schema[];
191
- anyOf?: Schema[];
192
- oneOf?: Schema[];
193
- not?: Schema;
194
- title?: string;
195
- description?: CommonMark;
196
- default?: unknown;
197
- format?: string;
198
- }
199
- export interface Discriminator {
200
- propertyName?: string;
201
- mapping?: Record<string, string>;
202
- }
203
- export interface XML extends Extendable {
204
- name?: string;
205
- namespace?: string;
206
- prefix?: string;
207
- attribute?: boolean;
208
- wrapped?: boolean;
209
- }
210
- export interface SecurityScheme extends Extendable {
211
- type: string;
212
- description?: string;
213
- name: string;
214
- in: string;
215
- schema: string;
216
- bearerFormat?: string;
217
- flows: OAuthFlows;
218
- openIdConnectUrl: string;
219
- }
220
- export interface OAuthFlows extends Extendable {
221
- implicit?: OAuthFlow;
222
- password?: OAuthFlow;
223
- clientCredentials?: OAuthFlow;
224
- authorizationCode?: OAuthFlow;
225
- }
226
- export interface OAuthFlow extends Extendable {
227
- authorizationUrl: string;
228
- tokenUrl: string;
229
- refreshUrl?: string;
230
- scopes: Record<string, string>;
231
- }
232
- export interface SecurityRequirement {
233
- [name: string]: string;
234
- }
235
- export type Extendable = {};
236
- export type CommonMark = string;
237
- export type Expression = string;
238
- export interface TypedSchema<T> extends Schema {
239
- allOf?: TypedSchema<T>[];
240
- anyOf?: TypedSchema<T>[];
241
- oneOf?: TypedSchema<T>[];
242
- not?: TypedSchema<T>;
243
- example?: T;
244
- enum?: T[];
245
- default?: T;
246
- }
247
- export interface StringSchema extends TypedSchema<string> {
248
- type: 'string';
249
- }
250
- export interface NumberSchema extends TypedSchema<number> {
251
- type: 'number';
252
- }
253
- export interface BooleanSchema extends TypedSchema<boolean> {
254
- type: 'boolean';
255
- }
256
- export interface ObjectSchema extends TypedSchema<Record<string, unknown>> {
257
- type: 'object';
258
- }
259
- export interface ArraySchema extends TypedSchema<unknown[]> {
260
- type: 'array';
261
- }
@@ -1,5 +0,0 @@
1
- /**
2
- * OpenAPI Specification
3
- * https://swagger.io/specification/
4
- */
5
- export {};
@@ -1,9 +0,0 @@
1
- import { Validator } from './common.js';
2
- export interface ArrayOptions {
3
- item: Validator;
4
- min?: number;
5
- max?: number;
6
- }
7
- export declare class ArrayValidator extends Validator<ArrayOptions> {
8
- validate(fieldName: string, value: unknown): import("@anjianshi/utils").Success<unknown> | import("@anjianshi/utils").Failed<string>;
9
- }
@@ -1,28 +0,0 @@
1
- import { success, failed } from '@anjianshi/utils';
2
- import { Validator } from './common.js';
3
- export class ArrayValidator extends Validator {
4
- validate(fieldName, value) {
5
- const superResult = super.validate(fieldName, value);
6
- if (!superResult.success)
7
- return superResult;
8
- value = superResult.data;
9
- if (value === null || value === undefined)
10
- return superResult;
11
- const opt = this.options;
12
- if (!Array.isArray(value))
13
- return failed(`${fieldName} should be an array`);
14
- if (typeof opt.min === 'number' && value.length < opt.min)
15
- return failed(`array ${fieldName}'s length should >= ${opt.min}`);
16
- if (typeof opt.max === 'number' && value.length > opt.max)
17
- return failed(`array ${fieldName}'s length should <= ${opt.max}`);
18
- const formatted = [];
19
- for (let i = 0; i < value.length; i++) {
20
- const itemResult = opt.item.validate(`${fieldName}[${i}]`, value[i]);
21
- if (itemResult.success)
22
- formatted.push(itemResult.data);
23
- else
24
- return itemResult;
25
- }
26
- return success(formatted);
27
- }
28
- }
@@ -1,4 +0,0 @@
1
- import { Validator } from './common.js';
2
- export declare class BooleanValidator extends Validator {
3
- validate(fieldName: string, value: unknown): import("@anjianshi/utils").Success<unknown> | import("@anjianshi/utils").Failed<string>;
4
- }
@@ -1,28 +0,0 @@
1
- import { success, failed } from '@anjianshi/utils';
2
- import { Validator } from './common.js';
3
- export class BooleanValidator extends Validator {
4
- validate(fieldName, value) {
5
- const superResult = super.validate(fieldName, value);
6
- if (!superResult.success)
7
- return superResult;
8
- value = superResult.data;
9
- if (value === null || value === undefined)
10
- return superResult;
11
- if (typeof value === 'string') {
12
- const str = value.trim().toLocaleLowerCase();
13
- if (['1', 'true', 'on', 'yes'].includes(str))
14
- value = true;
15
- else if (['0', 'false', 'off', 'no'].includes(str))
16
- value = false;
17
- }
18
- else if (typeof value === 'number') {
19
- if (value === 1)
20
- value = true;
21
- else if (value === 0)
22
- value = false;
23
- }
24
- if (typeof value !== 'boolean')
25
- return failed(`${fieldName} must be true or false`);
26
- return success(value);
27
- }
28
- }
@@ -1,23 +0,0 @@
1
- import { type MaySuccess } from '@anjianshi/utils';
2
- export interface CommonOptions {
3
- /**
4
- * allow `null`
5
- * @default false
6
- */
7
- null: boolean;
8
- /**
9
- * allow `undefined`
10
- * @default false
11
- */
12
- void: boolean;
13
- /**
14
- * 默认值,原值为 undefined 时生效(原值为 null 不会生效),指定后 void 选项将失去作用。
15
- * default value,used when original value is `undefined`(not for null), and make `void` option no effect.
16
- */
17
- defaults: unknown;
18
- }
19
- export declare class Validator<Opt = unknown> {
20
- readonly options: Opt & CommonOptions;
21
- constructor(options: Opt & Partial<CommonOptions>);
22
- validate(fieldName: string, value: unknown): MaySuccess<unknown>;
23
- }
@@ -1,25 +0,0 @@
1
- import { success, failed } from '@anjianshi/utils';
2
- export class Validator {
3
- options;
4
- constructor(options) {
5
- this.options = {
6
- null: false,
7
- void: false,
8
- defaults: undefined,
9
- ...options,
10
- };
11
- }
12
- validate(fieldName, value) {
13
- if (typeof value === 'undefined') {
14
- if (typeof this.options.defaults !== 'undefined') {
15
- value = this.options.defaults;
16
- }
17
- else if (!this.options.void) {
18
- return failed(`${fieldName} must have a value`);
19
- }
20
- }
21
- if (value === null && !this.options.null)
22
- return failed(`${fieldName} cannot be null`);
23
- return success(value);
24
- }
25
- }
@@ -1,20 +0,0 @@
1
- /**
2
- * 实现数据验证、格式化
3
- * Implement data validation and formatting
4
- */
5
- import { ArrayValidator } from './array.js';
6
- import { BooleanValidator } from './boolean.js';
7
- import { Validator, type CommonOptions } from './common.js';
8
- import { NumberValidator } from './number.js';
9
- import { ObjectValidator } from './object.js';
10
- import { StringValidator } from './string.js';
11
- /**
12
- * 仅检查空值,不检查具体格式
13
- * Check only for empty values, not for specific formatting
14
- */
15
- export declare function any(options?: Partial<CommonOptions>): Validator<unknown>;
16
- export declare function string(...args: ConstructorParameters<typeof StringValidator>): StringValidator;
17
- export declare function number(...args: ConstructorParameters<typeof NumberValidator>): NumberValidator;
18
- export declare function boolean(options?: Partial<CommonOptions>): BooleanValidator;
19
- export declare function array(itemValidator: Validator, options?: Omit<ConstructorParameters<typeof ArrayValidator>[0], 'item'>): ArrayValidator;
20
- export declare function object(structOrValue: Record<string, Validator> | Validator, options?: Partial<CommonOptions>): ObjectValidator;
@@ -1,38 +0,0 @@
1
- /**
2
- * 实现数据验证、格式化
3
- * Implement data validation and formatting
4
- */
5
- import { ArrayValidator } from './array.js';
6
- import { BooleanValidator } from './boolean.js';
7
- import { Validator } from './common.js';
8
- import { NumberValidator } from './number.js';
9
- import { ObjectValidator } from './object.js';
10
- import { StringValidator } from './string.js';
11
- /**
12
- * 仅检查空值,不检查具体格式
13
- * Check only for empty values, not for specific formatting
14
- */
15
- export function any(options) {
16
- return new Validator(options ?? {});
17
- }
18
- export function string(...args) {
19
- return new StringValidator(...args);
20
- }
21
- export function number(...args) {
22
- return new NumberValidator(...args);
23
- }
24
- export function boolean(options) {
25
- return new BooleanValidator(options ?? {});
26
- }
27
- export function array(itemValidator, options) {
28
- return new ArrayValidator({
29
- item: itemValidator,
30
- ...(options ?? {}),
31
- });
32
- }
33
- export function object(structOrValue, options) {
34
- return new ObjectValidator({
35
- ...(structOrValue instanceof Validator ? { value: structOrValue } : { struct: structOrValue }),
36
- ...options,
37
- });
38
- }