schema-dsl 1.0.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.
- package/.eslintignore +10 -0
- package/.eslintrc.json +27 -0
- package/.github/CODE_OF_CONDUCT.md +45 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +57 -0
- package/.github/ISSUE_TEMPLATE/config.yml +11 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +45 -0
- package/.github/ISSUE_TEMPLATE/question.md +31 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +70 -0
- package/.github/SECURITY.md +184 -0
- package/.github/workflows/ci.yml +33 -0
- package/CHANGELOG.md +633 -0
- package/CONTRIBUTING.md +368 -0
- package/LICENSE +21 -0
- package/README.md +1184 -0
- package/STATUS.md +101 -0
- package/docs/FEATURE-INDEX.md +519 -0
- package/docs/INDEX.md +253 -0
- package/docs/api-reference.md +1096 -0
- package/docs/best-practices.md +672 -0
- package/docs/cache-manager.md +336 -0
- package/docs/design-philosophy.md +601 -0
- package/docs/dsl-syntax.md +653 -0
- package/docs/dynamic-locale.md +552 -0
- package/docs/error-handling.md +703 -0
- package/docs/export-guide.md +462 -0
- package/docs/export-limitations.md +551 -0
- package/docs/faq.md +577 -0
- package/docs/frontend-i18n-guide.md +290 -0
- package/docs/i18n-user-guide.md +476 -0
- package/docs/label-vs-description.md +262 -0
- package/docs/markdown-exporter.md +397 -0
- package/docs/mongodb-exporter.md +295 -0
- package/docs/multi-type-support.md +319 -0
- package/docs/mysql-exporter.md +273 -0
- package/docs/plugin-system.md +542 -0
- package/docs/postgresql-exporter.md +304 -0
- package/docs/quick-start.md +761 -0
- package/docs/schema-helper.md +340 -0
- package/docs/schema-utils-chaining.md +143 -0
- package/docs/schema-utils.md +490 -0
- package/docs/string-extensions.md +480 -0
- package/docs/troubleshooting.md +471 -0
- package/docs/type-converter.md +319 -0
- package/docs/type-reference.md +219 -0
- package/docs/validate-async.md +480 -0
- package/docs/validate.md +486 -0
- package/docs/validation-guide.md +484 -0
- package/examples/array-dsl-example.js +227 -0
- package/examples/custom-extension.js +85 -0
- package/examples/dsl-match-example.js +74 -0
- package/examples/dsl-style.js +118 -0
- package/examples/dynamic-locale-configuration.js +348 -0
- package/examples/dynamic-locale-example.js +287 -0
- package/examples/export-demo.js +130 -0
- package/examples/express-integration.js +376 -0
- package/examples/i18n-full-demo.js +310 -0
- package/examples/i18n-memory-safety.examples.js +268 -0
- package/examples/markdown-export.js +71 -0
- package/examples/middleware-usage.js +93 -0
- package/examples/new-features-comparison.js +315 -0
- package/examples/password-reset/README.md +153 -0
- package/examples/password-reset/schema.js +26 -0
- package/examples/password-reset/test.js +101 -0
- package/examples/plugin-system.examples.js +205 -0
- package/examples/schema-utils-chaining.examples.js +250 -0
- package/examples/simple-example.js +122 -0
- package/examples/string-extensions.js +297 -0
- package/examples/user-registration/README.md +156 -0
- package/examples/user-registration/routes.js +92 -0
- package/examples/user-registration/schema.js +150 -0
- package/examples/user-registration/server.js +74 -0
- package/index.d.ts +1999 -0
- package/index.js +282 -0
- package/index.mjs +30 -0
- package/lib/adapters/DslAdapter.js +699 -0
- package/lib/adapters/index.js +20 -0
- package/lib/config/constants.js +286 -0
- package/lib/config/patterns/creditCard.js +9 -0
- package/lib/config/patterns/idCard.js +9 -0
- package/lib/config/patterns/index.js +8 -0
- package/lib/config/patterns/licensePlate.js +4 -0
- package/lib/config/patterns/passport.js +4 -0
- package/lib/config/patterns/phone.js +9 -0
- package/lib/config/patterns/postalCode.js +5 -0
- package/lib/core/CacheManager.js +376 -0
- package/lib/core/DslBuilder.js +740 -0
- package/lib/core/ErrorCodes.js +233 -0
- package/lib/core/ErrorFormatter.js +342 -0
- package/lib/core/JSONSchemaCore.js +347 -0
- package/lib/core/Locale.js +119 -0
- package/lib/core/MessageTemplate.js +89 -0
- package/lib/core/PluginManager.js +448 -0
- package/lib/core/StringExtensions.js +209 -0
- package/lib/core/Validator.js +376 -0
- package/lib/errors/ValidationError.js +191 -0
- package/lib/exporters/MarkdownExporter.js +420 -0
- package/lib/exporters/MongoDBExporter.js +162 -0
- package/lib/exporters/MySQLExporter.js +212 -0
- package/lib/exporters/PostgreSQLExporter.js +289 -0
- package/lib/exporters/index.js +24 -0
- package/lib/locales/en-US.js +65 -0
- package/lib/locales/es-ES.js +66 -0
- package/lib/locales/fr-FR.js +66 -0
- package/lib/locales/index.js +8 -0
- package/lib/locales/ja-JP.js +66 -0
- package/lib/locales/zh-CN.js +93 -0
- package/lib/utils/LRUCache.js +174 -0
- package/lib/utils/SchemaHelper.js +240 -0
- package/lib/utils/SchemaUtils.js +445 -0
- package/lib/utils/TypeConverter.js +245 -0
- package/lib/utils/index.js +13 -0
- package/lib/validators/CustomKeywords.js +203 -0
- package/lib/validators/index.js +11 -0
- package/package.json +70 -0
- package/plugins/custom-format.js +101 -0
- package/plugins/custom-validator.js +200 -0
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
# TypeConverter 类型转换工具
|
|
2
|
+
|
|
3
|
+
> **模块**: `lib/utils/TypeConverter.js`
|
|
4
|
+
|
|
5
|
+
> **用途**: 提供 JSON Schema 与各种数据库类型之间的转换
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 📑 目录
|
|
10
|
+
|
|
11
|
+
- [概述](#概述)
|
|
12
|
+
- [快速开始](#快速开始)
|
|
13
|
+
- [API 参考](#api-参考)
|
|
14
|
+
- [类型映射表](#类型映射表)
|
|
15
|
+
- [实用示例](#实用示例)
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## 概述
|
|
20
|
+
|
|
21
|
+
`TypeConverter` 是一个静态工具类,用于在 JSON Schema 类型与各种数据库类型之间进行转换。它是所有导出器的基础依赖。
|
|
22
|
+
|
|
23
|
+
### 核心功能
|
|
24
|
+
|
|
25
|
+
- ✅ JSON Schema ↔ MongoDB BSON 类型转换
|
|
26
|
+
- ✅ JSON Schema ↔ MySQL 类型转换
|
|
27
|
+
- ✅ JSON Schema ↔ PostgreSQL 类型转换
|
|
28
|
+
- ✅ 属性名规范化(驼峰 ↔ 下划线)
|
|
29
|
+
- ✅ 格式验证正则表达式
|
|
30
|
+
- ✅ Schema 合并与约束提取
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## 快速开始
|
|
35
|
+
|
|
36
|
+
```javascript
|
|
37
|
+
const { TypeConverter } = require('schema-dsl/lib/utils');
|
|
38
|
+
|
|
39
|
+
// JSON Schema 类型转 MongoDB 类型
|
|
40
|
+
const mongoType = TypeConverter.toMongoDBType('integer');
|
|
41
|
+
console.log(mongoType); // 'int'
|
|
42
|
+
|
|
43
|
+
// JSON Schema 类型转 MySQL 类型
|
|
44
|
+
const mysqlType = TypeConverter.toMySQLType('string', { maxLength: 100 });
|
|
45
|
+
console.log(mysqlType); // 'VARCHAR(100)'
|
|
46
|
+
|
|
47
|
+
// JSON Schema 类型转 PostgreSQL 类型
|
|
48
|
+
const pgType = TypeConverter.toPostgreSQLType('string', { format: 'uuid' });
|
|
49
|
+
console.log(pgType); // 'UUID'
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
---
|
|
53
|
+
|
|
54
|
+
## API 参考
|
|
55
|
+
|
|
56
|
+
### `toJSONSchemaType(simpleType)`
|
|
57
|
+
|
|
58
|
+
将简单类型字符串转换为 JSON Schema 类型对象。
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
TypeConverter.toJSONSchemaType('string');
|
|
62
|
+
// { type: 'string' }
|
|
63
|
+
|
|
64
|
+
TypeConverter.toJSONSchemaType('int');
|
|
65
|
+
// { type: 'integer' }
|
|
66
|
+
|
|
67
|
+
TypeConverter.toJSONSchemaType('bool');
|
|
68
|
+
// { type: 'boolean' }
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**支持的别名**:
|
|
72
|
+
|
|
73
|
+
| 完整类型 | 别名 |
|
|
74
|
+
|---------|------|
|
|
75
|
+
| `string` | `str`, `s` |
|
|
76
|
+
| `number` | `num`, `n` |
|
|
77
|
+
| `integer` | `int`, `i` |
|
|
78
|
+
| `boolean` | `bool`, `b` |
|
|
79
|
+
| `object` | `obj`, `o` |
|
|
80
|
+
| `array` | `arr`, `a` |
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
### `toMongoDBType(jsonSchemaType)`
|
|
85
|
+
|
|
86
|
+
JSON Schema 类型转 MongoDB BSON 类型。
|
|
87
|
+
|
|
88
|
+
```javascript
|
|
89
|
+
TypeConverter.toMongoDBType('string'); // 'string'
|
|
90
|
+
TypeConverter.toMongoDBType('number'); // 'double'
|
|
91
|
+
TypeConverter.toMongoDBType('integer'); // 'int'
|
|
92
|
+
TypeConverter.toMongoDBType('boolean'); // 'bool'
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
### `toMySQLType(jsonSchemaType, constraints)`
|
|
98
|
+
|
|
99
|
+
JSON Schema 类型转 MySQL 数据类型。
|
|
100
|
+
|
|
101
|
+
```javascript
|
|
102
|
+
// 基本转换
|
|
103
|
+
TypeConverter.toMySQLType('string');
|
|
104
|
+
// 'VARCHAR(255)'
|
|
105
|
+
|
|
106
|
+
// 带长度约束
|
|
107
|
+
TypeConverter.toMySQLType('string', { maxLength: 50 });
|
|
108
|
+
// 'VARCHAR(50)'
|
|
109
|
+
|
|
110
|
+
// 长文本
|
|
111
|
+
TypeConverter.toMySQLType('string', { maxLength: 500 });
|
|
112
|
+
// 'TEXT'
|
|
113
|
+
|
|
114
|
+
// 邮箱格式
|
|
115
|
+
TypeConverter.toMySQLType('string', { format: 'email' });
|
|
116
|
+
// 'VARCHAR(255)'
|
|
117
|
+
|
|
118
|
+
// 整数范围
|
|
119
|
+
TypeConverter.toMySQLType('integer', { maximum: 100 });
|
|
120
|
+
// 'TINYINT'
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
### `toPostgreSQLType(jsonSchemaType, constraints)`
|
|
126
|
+
|
|
127
|
+
JSON Schema 类型转 PostgreSQL 数据类型。
|
|
128
|
+
|
|
129
|
+
```javascript
|
|
130
|
+
// UUID 格式
|
|
131
|
+
TypeConverter.toPostgreSQLType('string', { format: 'uuid' });
|
|
132
|
+
// 'UUID'
|
|
133
|
+
|
|
134
|
+
// 日期时间
|
|
135
|
+
TypeConverter.toPostgreSQLType('string', { format: 'date-time' });
|
|
136
|
+
// 'TIMESTAMP'
|
|
137
|
+
|
|
138
|
+
// JSON 对象
|
|
139
|
+
TypeConverter.toPostgreSQLType('object');
|
|
140
|
+
// 'JSONB'
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
### `normalizePropertyName(name, style)`
|
|
146
|
+
|
|
147
|
+
规范化属性名,转换命名风格。
|
|
148
|
+
|
|
149
|
+
```javascript
|
|
150
|
+
// camelCase 转 snake_case
|
|
151
|
+
TypeConverter.normalizePropertyName('userName', 'snake_case');
|
|
152
|
+
// 'user_name'
|
|
153
|
+
|
|
154
|
+
TypeConverter.normalizePropertyName('createdAt', 'snake_case');
|
|
155
|
+
// 'created_at'
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
### `formatToRegex(format)`
|
|
161
|
+
|
|
162
|
+
获取格式对应的正则表达式。
|
|
163
|
+
|
|
164
|
+
```javascript
|
|
165
|
+
TypeConverter.formatToRegex('email');
|
|
166
|
+
// '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$'
|
|
167
|
+
|
|
168
|
+
TypeConverter.formatToRegex('uuid');
|
|
169
|
+
// '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
|
|
170
|
+
|
|
171
|
+
TypeConverter.formatToRegex('ipv4');
|
|
172
|
+
// IPv4 正则表达式
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
### `mergeSchemas(base, override)`
|
|
178
|
+
|
|
179
|
+
合并两个 JSON Schema 对象。
|
|
180
|
+
|
|
181
|
+
```javascript
|
|
182
|
+
const base = {
|
|
183
|
+
type: 'object',
|
|
184
|
+
properties: { name: { type: 'string' } },
|
|
185
|
+
required: ['name']
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const override = {
|
|
189
|
+
properties: { email: { type: 'string' } },
|
|
190
|
+
required: ['email']
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const merged = TypeConverter.mergeSchemas(base, override);
|
|
194
|
+
// {
|
|
195
|
+
// type: 'object',
|
|
196
|
+
// properties: { name: {...}, email: {...} },
|
|
197
|
+
// required: ['name', 'email']
|
|
198
|
+
// }
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
### `extractConstraints(schema)`
|
|
204
|
+
|
|
205
|
+
提取 Schema 中的约束条件。
|
|
206
|
+
|
|
207
|
+
```javascript
|
|
208
|
+
const schema = {
|
|
209
|
+
type: 'string',
|
|
210
|
+
minLength: 3,
|
|
211
|
+
maxLength: 32,
|
|
212
|
+
pattern: '^[a-z]+$',
|
|
213
|
+
format: 'email'
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const constraints = TypeConverter.extractConstraints(schema);
|
|
217
|
+
// {
|
|
218
|
+
// minLength: 3,
|
|
219
|
+
// maxLength: 32,
|
|
220
|
+
// minimum: undefined,
|
|
221
|
+
// maximum: undefined,
|
|
222
|
+
// pattern: '^[a-z]+$',
|
|
223
|
+
// format: 'email',
|
|
224
|
+
// enum: undefined,
|
|
225
|
+
// default: undefined
|
|
226
|
+
// }
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
---
|
|
230
|
+
|
|
231
|
+
## 类型映射表
|
|
232
|
+
|
|
233
|
+
### JSON Schema → MongoDB
|
|
234
|
+
|
|
235
|
+
| JSON Schema | MongoDB BSON |
|
|
236
|
+
|-------------|--------------|
|
|
237
|
+
| `string` | `string` |
|
|
238
|
+
| `number` | `double` |
|
|
239
|
+
| `integer` | `int` |
|
|
240
|
+
| `boolean` | `bool` |
|
|
241
|
+
| `object` | `object` |
|
|
242
|
+
| `array` | `array` |
|
|
243
|
+
| `null` | `null` |
|
|
244
|
+
|
|
245
|
+
### JSON Schema → MySQL
|
|
246
|
+
|
|
247
|
+
| JSON Schema | 约束 | MySQL |
|
|
248
|
+
|-------------|------|-------|
|
|
249
|
+
| `string` | - | `VARCHAR(255)` |
|
|
250
|
+
| `string` | `maxLength: 50` | `VARCHAR(50)` |
|
|
251
|
+
| `string` | `maxLength: 500` | `TEXT` |
|
|
252
|
+
| `string` | `format: email` | `VARCHAR(255)` |
|
|
253
|
+
| `string` | `format: date-time` | `DATETIME` |
|
|
254
|
+
| `integer` | `maximum: 127` | `TINYINT` |
|
|
255
|
+
| `integer` | `maximum: 32767` | `SMALLINT` |
|
|
256
|
+
| `integer` | `maximum: 2147483647` | `INT` |
|
|
257
|
+
| `integer` | - | `BIGINT` |
|
|
258
|
+
| `number` | - | `DOUBLE` |
|
|
259
|
+
| `boolean` | - | `BOOLEAN` |
|
|
260
|
+
| `object` | - | `JSON` |
|
|
261
|
+
| `array` | - | `JSON` |
|
|
262
|
+
|
|
263
|
+
### JSON Schema → PostgreSQL
|
|
264
|
+
|
|
265
|
+
| JSON Schema | 约束 | PostgreSQL |
|
|
266
|
+
|-------------|------|------------|
|
|
267
|
+
| `string` | - | `VARCHAR(255)` |
|
|
268
|
+
| `string` | `maxLength: 50` | `VARCHAR(50)` |
|
|
269
|
+
| `string` | `maxLength: 500` | `TEXT` |
|
|
270
|
+
| `string` | `format: uuid` | `UUID` |
|
|
271
|
+
| `string` | `format: date` | `DATE` |
|
|
272
|
+
| `string` | `format: date-time` | `TIMESTAMP` |
|
|
273
|
+
| `integer` | `maximum: 32767` | `SMALLINT` |
|
|
274
|
+
| `integer` | `maximum: 2147483647` | `INTEGER` |
|
|
275
|
+
| `integer` | - | `BIGINT` |
|
|
276
|
+
| `number` | - | `DOUBLE PRECISION` |
|
|
277
|
+
| `boolean` | - | `BOOLEAN` |
|
|
278
|
+
| `object` | - | `JSONB` |
|
|
279
|
+
| `array` | - | `JSONB` |
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
## 实用示例
|
|
284
|
+
|
|
285
|
+
### 批量类型转换
|
|
286
|
+
|
|
287
|
+
```javascript
|
|
288
|
+
const { TypeConverter } = require('schema-dsl/lib/utils');
|
|
289
|
+
|
|
290
|
+
const fields = ['string', 'number', 'integer', 'boolean', 'object', 'array'];
|
|
291
|
+
|
|
292
|
+
console.log('=== 类型映射对比 ===');
|
|
293
|
+
fields.forEach(type => {
|
|
294
|
+
console.log(`${type}:`);
|
|
295
|
+
console.log(` MongoDB: ${TypeConverter.toMongoDBType(type)}`);
|
|
296
|
+
console.log(` MySQL: ${TypeConverter.toMySQLType(type)}`);
|
|
297
|
+
console.log(` PostgreSQL: ${TypeConverter.toPostgreSQLType(type)}`);
|
|
298
|
+
});
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
### 格式验证
|
|
302
|
+
|
|
303
|
+
```javascript
|
|
304
|
+
const emailRegex = TypeConverter.formatToRegex('email');
|
|
305
|
+
const regex = new RegExp(emailRegex);
|
|
306
|
+
|
|
307
|
+
console.log(regex.test('user@example.com')); // true
|
|
308
|
+
console.log(regex.test('invalid-email')); // false
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
---
|
|
312
|
+
|
|
313
|
+
## 相关文档
|
|
314
|
+
|
|
315
|
+
- [SchemaHelper](schema-helper.md)
|
|
316
|
+
- [MongoDB 导出器](mongodb-exporter.md)
|
|
317
|
+
- [MySQL 导出器](mysql-exporter.md)
|
|
318
|
+
- [PostgreSQL 导出器](postgresql-exporter.md)
|
|
319
|
+
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
# schema-dsl 完整类型列表
|
|
2
|
+
|
|
3
|
+
> **更新时间**: 2025-12-25
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 📊 支持的类型
|
|
8
|
+
|
|
9
|
+
### 基本类型
|
|
10
|
+
|
|
11
|
+
| 类型 | SchemaIO | JSON Schema | 说明 |
|
|
12
|
+
|------|----------|-------------|------|
|
|
13
|
+
| 字符串 | `string` | `{ type: 'string' }` | 文本类型 |
|
|
14
|
+
| 数字 | `number` | `{ type: 'number' }` | 浮点数 |
|
|
15
|
+
| 整数 | `integer` | `{ type: 'integer' }` | 整数 |
|
|
16
|
+
| 布尔 | `boolean` | `{ type: 'boolean' }` | true/false |
|
|
17
|
+
| 对象 | `object` | `{ type: 'object' }` | 嵌套对象 |
|
|
18
|
+
| 数组 | `array` | `{ type: 'array' }` | 数组 |
|
|
19
|
+
| 空值 | `null` | `{ type: 'null' }` | null值 |
|
|
20
|
+
| 任意 | `any` | `{}` | 任意类型 |
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
### 格式类型(基于 string)
|
|
25
|
+
|
|
26
|
+
| 类型 | SchemaIO | JSON Schema format | 说明 |
|
|
27
|
+
|------|----------|-------------------|------|
|
|
28
|
+
| 邮箱 | `email` | `email` | 邮箱地址 |
|
|
29
|
+
| URL | `url` | `uri` | 网址 |
|
|
30
|
+
| UUID | `uuid` | `uuid` | UUID格式 |
|
|
31
|
+
| 日期 | `date` | `date` | YYYY-MM-DD |
|
|
32
|
+
| 日期时间 | `datetime` | `date-time` | ISO 8601 |
|
|
33
|
+
| 时间 | `time` | `time` | HH:mm:ss |
|
|
34
|
+
| IPv4 | `ipv4` | `ipv4` | IPv4地址 |
|
|
35
|
+
| IPv6 | `ipv6` | `ipv6` | IPv6地址 |
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
### 特殊类型
|
|
40
|
+
|
|
41
|
+
| 类型 | SchemaIO | JSON Schema | 说明 |
|
|
42
|
+
|------|----------|-------------|------|
|
|
43
|
+
| 二进制 | `binary` | `contentEncoding: base64` | Base64编码 |
|
|
44
|
+
| ObjectId | `objectId` | `pattern: ^[0-9a-fA-F]{24}$` | MongoDB ObjectId |
|
|
45
|
+
| HexColor | `hexColor` | `pattern: ^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$` | CSS 16进制颜色 |
|
|
46
|
+
| MAC地址 | `macAddress` | `pattern: ^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$` | MAC地址 |
|
|
47
|
+
| Cron | `cron` | `pattern: ...` | Cron表达式 |
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## 📝 类型使用示例
|
|
52
|
+
|
|
53
|
+
### 基本类型
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
const { dsl } = require('schema-dsl');
|
|
57
|
+
|
|
58
|
+
// 字符串
|
|
59
|
+
const schema1 = dsl({ name: 'string' });
|
|
60
|
+
|
|
61
|
+
// 数字
|
|
62
|
+
const schema2 = dsl({ age: 'number' });
|
|
63
|
+
|
|
64
|
+
// 整数
|
|
65
|
+
const schema3 = dsl({ count: 'integer' });
|
|
66
|
+
|
|
67
|
+
// 布尔
|
|
68
|
+
const schema4 = dsl({ active: 'boolean' });
|
|
69
|
+
|
|
70
|
+
// 对象
|
|
71
|
+
const schema5 = dsl({
|
|
72
|
+
user: {
|
|
73
|
+
name: 'string',
|
|
74
|
+
age: 'number'
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// 数组
|
|
79
|
+
const schema6 = dsl({ tags: 'array<string>' });
|
|
80
|
+
|
|
81
|
+
// 空值
|
|
82
|
+
const schema7 = dsl({ value: 'null' });
|
|
83
|
+
|
|
84
|
+
// 任意类型
|
|
85
|
+
const schema8 = dsl({ data: 'any' });
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
### 扩展验证类型
|
|
91
|
+
|
|
92
|
+
```javascript
|
|
93
|
+
// 手机号
|
|
94
|
+
const schema1 = dsl({ mobile: 'phone:cn!' });
|
|
95
|
+
|
|
96
|
+
// 身份证
|
|
97
|
+
const schema2 = dsl({ id_card: 'idCard:cn!' });
|
|
98
|
+
|
|
99
|
+
// 信用卡
|
|
100
|
+
const schema3 = dsl({ card: 'creditCard:visa!' });
|
|
101
|
+
|
|
102
|
+
// 车牌号
|
|
103
|
+
const schema4 = dsl({ plate: 'licensePlate:cn!' });
|
|
104
|
+
|
|
105
|
+
// 邮政编码
|
|
106
|
+
const schema5 = dsl({ zip: 'postalCode:cn!' });
|
|
107
|
+
|
|
108
|
+
// 护照
|
|
109
|
+
const schema6 = dsl({ passport: 'passport:cn!' });
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
### 格式类型
|
|
115
|
+
|
|
116
|
+
```javascript
|
|
117
|
+
// 邮箱
|
|
118
|
+
const schema1 = dsl({ email: 'email!' });
|
|
119
|
+
|
|
120
|
+
// URL
|
|
121
|
+
const schema2 = dsl({ website: 'url' });
|
|
122
|
+
|
|
123
|
+
// UUID
|
|
124
|
+
const schema3 = dsl({ id: 'uuid!' });
|
|
125
|
+
|
|
126
|
+
// 日期
|
|
127
|
+
const schema4 = dsl({ birthday: 'date' });
|
|
128
|
+
|
|
129
|
+
// 日期时间
|
|
130
|
+
const schema5 = dsl({ created_at: 'datetime!' });
|
|
131
|
+
|
|
132
|
+
// 时间
|
|
133
|
+
const schema6 = dsl({ start_time: 'time' });
|
|
134
|
+
|
|
135
|
+
// IP地址
|
|
136
|
+
const schema7 = dsl({
|
|
137
|
+
ipv4_addr: 'ipv4',
|
|
138
|
+
ipv6_addr: 'ipv6'
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
### 特殊类型
|
|
145
|
+
|
|
146
|
+
```javascript
|
|
147
|
+
// 二进制数据(Base64)
|
|
148
|
+
const schema = dsl({
|
|
149
|
+
avatar: 'binary' // 头像图片(Base64编码)
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
## 🔄 与 joi 的对应关系
|
|
156
|
+
|
|
157
|
+
### 完整对照表
|
|
158
|
+
|
|
159
|
+
| joi | SchemaIO DSL | 说明 |
|
|
160
|
+
|-----|--------------|------|
|
|
161
|
+
| `Joi.string()` | `'string'` | 字符串 |
|
|
162
|
+
| `Joi.string().email()` | `'email'` | 邮箱 |
|
|
163
|
+
| `Joi.string().uri()` | `'url'` | URL |
|
|
164
|
+
| `Joi.string().uuid()` | `'uuid'` | UUID |
|
|
165
|
+
| `Joi.string().ip()` | `'ipv4'` 或 `'ipv6'` | IP地址 |
|
|
166
|
+
| `Joi.string().min(3).max(32)` | `'string:3-32'` | 长度范围 |
|
|
167
|
+
| `Joi.string().required()` | `'string!'` | 必填 |
|
|
168
|
+
| `Joi.number()` | `'number'` | 数字 |
|
|
169
|
+
| `Joi.number().min(0).max(100)` | `'number:0-100'` | 数字范围 |
|
|
170
|
+
| `Joi.number().integer()` | `'integer'` | 整数 |
|
|
171
|
+
| `Joi.boolean()` | `'boolean'` | 布尔 |
|
|
172
|
+
| `Joi.date()` | `'date'` 或 `'datetime'` | 日期 |
|
|
173
|
+
| `Joi.array()` | `'array'` | 数组 |
|
|
174
|
+
| `Joi.array().items(Joi.string())` | `'array<string>'` | 字符串数组 |
|
|
175
|
+
| `Joi.array().min(1).max(10)` | `'array:1-10'` | 数组长度 |
|
|
176
|
+
| `Joi.object()` | `{ ... }` | 对象 |
|
|
177
|
+
| `Joi.any()` | `'any'` | 任意类型 |
|
|
178
|
+
| `Joi.binary()` | `'binary'` | 二进制 |
|
|
179
|
+
| `Joi.valid('a','b','c')` | `'a\|b\|c'` | 枚举 |
|
|
180
|
+
|
|
181
|
+
---
|
|
182
|
+
|
|
183
|
+
## 📚 相关文档
|
|
184
|
+
|
|
185
|
+
- [DSL 语法指南](./dsl-syntax.md) - 完整语法说明
|
|
186
|
+
- [快速开始](./quick-start.md) - 5分钟上手
|
|
187
|
+
- [String 扩展](./string-extensions.md) - 链式调用
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## ❓ 常见问题
|
|
192
|
+
|
|
193
|
+
### Q1: 为什么没有 `Joi.alternatives()` 对应?
|
|
194
|
+
|
|
195
|
+
A: 使用条件验证 `dsl.match()` 实现:
|
|
196
|
+
|
|
197
|
+
```javascript
|
|
198
|
+
const schema = dsl({
|
|
199
|
+
contactType: 'email|phone',
|
|
200
|
+
contact: dsl.match('contactType', {
|
|
201
|
+
email: 'email!',
|
|
202
|
+
phone: 'string:11!'
|
|
203
|
+
})
|
|
204
|
+
});
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Q2: 为什么 `integer` 不是 `number().integer()`?
|
|
208
|
+
|
|
209
|
+
A: SchemaIO 使用 JSON Schema 标准,`integer` 是独立类型。
|
|
210
|
+
|
|
211
|
+
### Q3: 不支持简写吗?
|
|
212
|
+
|
|
213
|
+
A: 不支持 `s`/`n`/`i`/`b` 等简写,统一使用完整类型名(`string`/`number`/`integer`/`boolean`),降低学习成本。
|
|
214
|
+
|
|
215
|
+
---
|
|
216
|
+
|
|
217
|
+
**最后更新**: 2025-12-25
|
|
218
|
+
|
|
219
|
+
|