@gunubin/vorm-core 0.1.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/LICENSE +21 -0
- package/README.md +87 -0
- package/dist/index.cjs +242 -0
- package/dist/index.d.cts +117 -0
- package/dist/index.d.ts +117 -0
- package/dist/index.js +206 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 gunubin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# @gunubin/vorm-core
|
|
2
|
+
|
|
3
|
+
VO-first form validation core — branded types, field schemas, and validation logic.
|
|
4
|
+
|
|
5
|
+
Part of the [vorm](https://github.com/gunubin/vorm) monorepo.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @gunubin/vorm-core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
### Define Value Objects
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { vo } from '@gunubin/vorm-core';
|
|
19
|
+
|
|
20
|
+
const Email = vo('Email', [
|
|
21
|
+
{ code: 'INVALID_FORMAT', validate: (v: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v) },
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
Email.create('user@example.com'); // Brand<string, 'Email'>
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Create Fields and Form Schema
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { createField, createFormSchema } from '@gunubin/vorm-core';
|
|
31
|
+
|
|
32
|
+
const emailField = createField(Email);
|
|
33
|
+
|
|
34
|
+
const schema = createFormSchema({
|
|
35
|
+
fields: {
|
|
36
|
+
email: emailField({
|
|
37
|
+
required: true,
|
|
38
|
+
messages: { REQUIRED: 'Email is required', INVALID_FORMAT: 'Invalid email' },
|
|
39
|
+
}),
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Reusable Rules with `createRule`
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { createRule } from '@gunubin/vorm-core';
|
|
48
|
+
|
|
49
|
+
const minLength = createRule('TOO_SHORT', (v: string, min: number) => v.length >= min);
|
|
50
|
+
const maxLength = createRule('TOO_LONG', (v: string, max: number) => v.length <= max);
|
|
51
|
+
|
|
52
|
+
const Username = vo('Username', [minLength(3), maxLength(20)]);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Parse / Format
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
const priceField = createField<number>({
|
|
59
|
+
parse: (v: string) => Number(v.replace(/,/g, '')),
|
|
60
|
+
format: (v: number) => v.toLocaleString(),
|
|
61
|
+
})({ required: true });
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## API
|
|
65
|
+
|
|
66
|
+
| Export | Description |
|
|
67
|
+
|--------|-------------|
|
|
68
|
+
| `vo(brand, rules)` | Define a Value Object type with branded output |
|
|
69
|
+
| `createField(vo, options?)` | Create a field factory from a VO definition |
|
|
70
|
+
| `createField(config)` | Create a field schema directly for plain types |
|
|
71
|
+
| `createRule(code, validate)` | Create reusable, parameterized validation rules |
|
|
72
|
+
| `createFormSchema(config)` | Bundle fields into a form schema |
|
|
73
|
+
| `validateField(value, schema)` | Validate a single field |
|
|
74
|
+
| `validateForm(values, schema)` | Validate all fields |
|
|
75
|
+
| `VOValidationError` | Error thrown by `vo().create()` on failure |
|
|
76
|
+
|
|
77
|
+
### Types
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import type { Brand, Infer, ErrorMessageMap } from '@gunubin/vorm-core';
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
For full documentation, see the [vorm README](https://github.com/gunubin/vorm#readme).
|
|
84
|
+
|
|
85
|
+
## License
|
|
86
|
+
|
|
87
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
VOValidationError: () => VOValidationError,
|
|
24
|
+
buildOutputValues: () => buildOutputValues,
|
|
25
|
+
createField: () => createField,
|
|
26
|
+
createFormSchema: () => createFormSchema,
|
|
27
|
+
createRule: () => createRule,
|
|
28
|
+
safeValidateAndCreate: () => safeValidateAndCreate,
|
|
29
|
+
validateAndCreate: () => validateAndCreate,
|
|
30
|
+
validateField: () => validateField,
|
|
31
|
+
validateForm: () => validateForm,
|
|
32
|
+
vo: () => vo
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(index_exports);
|
|
35
|
+
|
|
36
|
+
// src/vo.ts
|
|
37
|
+
var VOValidationError = class extends Error {
|
|
38
|
+
constructor(brand, code, input) {
|
|
39
|
+
super(`${brand} is not valid (${code})`);
|
|
40
|
+
this.brand = brand;
|
|
41
|
+
this.code = code;
|
|
42
|
+
this.input = input;
|
|
43
|
+
this.name = "VOValidationError";
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
function vo(brand, rules) {
|
|
47
|
+
return {
|
|
48
|
+
brand,
|
|
49
|
+
rules,
|
|
50
|
+
create(input) {
|
|
51
|
+
for (const rule of rules) {
|
|
52
|
+
if (!rule.validate(input)) {
|
|
53
|
+
throw new VOValidationError(brand, rule.code, input);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return input;
|
|
57
|
+
},
|
|
58
|
+
safeCreate(input) {
|
|
59
|
+
for (const rule of rules) {
|
|
60
|
+
if (!rule.validate(input)) {
|
|
61
|
+
return { success: false, error: { code: rule.code } };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return { success: true, data: input };
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/create-rule.ts
|
|
70
|
+
function createRule(code, validate) {
|
|
71
|
+
return ((option) => ({
|
|
72
|
+
code,
|
|
73
|
+
validate: (value) => validate(value, option)
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/create-field.ts
|
|
78
|
+
function createField(voOrConfig, options) {
|
|
79
|
+
if (voOrConfig && "create" in voOrConfig && typeof voOrConfig.create === "function") {
|
|
80
|
+
let factory2 = function(config2 = {}) {
|
|
81
|
+
return {
|
|
82
|
+
vo: voDef,
|
|
83
|
+
required: config2.required ?? false,
|
|
84
|
+
messages: mergeMessages(definitionMessages2, config2.messages),
|
|
85
|
+
rules: [...voDef.rules],
|
|
86
|
+
parse: parse2,
|
|
87
|
+
format: format2
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
var factory = factory2;
|
|
91
|
+
const voDef = voOrConfig;
|
|
92
|
+
const definitionMessages2 = options?.messages;
|
|
93
|
+
const parse2 = options?.parse;
|
|
94
|
+
const format2 = options?.format;
|
|
95
|
+
return factory2;
|
|
96
|
+
}
|
|
97
|
+
const config = voOrConfig ?? {};
|
|
98
|
+
const definitionMessages = config.messages;
|
|
99
|
+
const definitionRules = config.rules ?? [];
|
|
100
|
+
const parse = config.parse;
|
|
101
|
+
const format = config.format;
|
|
102
|
+
function factory(factoryConfig = {}) {
|
|
103
|
+
return {
|
|
104
|
+
vo: null,
|
|
105
|
+
required: factoryConfig.required ?? false,
|
|
106
|
+
messages: mergeMessages(definitionMessages, factoryConfig.messages),
|
|
107
|
+
rules: [...definitionRules],
|
|
108
|
+
parse,
|
|
109
|
+
format
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
return factory;
|
|
113
|
+
}
|
|
114
|
+
function mergeMessages(definition, factory) {
|
|
115
|
+
if (!definition && !factory) return {};
|
|
116
|
+
if (!definition) return factory;
|
|
117
|
+
if (!factory) return definition;
|
|
118
|
+
if (typeof factory === "function") return factory;
|
|
119
|
+
if (typeof definition === "function") return factory;
|
|
120
|
+
return { ...definition, ...factory };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// src/create-form-schema.ts
|
|
124
|
+
function createFormSchema(config) {
|
|
125
|
+
return {
|
|
126
|
+
fields: config.fields,
|
|
127
|
+
messages: config.messages,
|
|
128
|
+
resolver: config.resolver
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// src/resolve-message.ts
|
|
133
|
+
var DEFAULT_MESSAGES = {
|
|
134
|
+
REQUIRED: "This field is required"
|
|
135
|
+
};
|
|
136
|
+
function resolveMessage(code, ...messageSources) {
|
|
137
|
+
for (const source of messageSources) {
|
|
138
|
+
if (!source) continue;
|
|
139
|
+
if (typeof source === "function") {
|
|
140
|
+
const result = source({ code });
|
|
141
|
+
if (result) return result;
|
|
142
|
+
} else {
|
|
143
|
+
const msg = source[code];
|
|
144
|
+
if (msg) return msg;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return DEFAULT_MESSAGES[code] ?? code;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// src/validate-field.ts
|
|
151
|
+
function validateField(value, fieldSchema, formMessages) {
|
|
152
|
+
if (fieldSchema.required) {
|
|
153
|
+
if (value === void 0 || value === null || value === "") {
|
|
154
|
+
const code = "REQUIRED";
|
|
155
|
+
const message = resolveMessage(code, formMessages, fieldSchema.messages);
|
|
156
|
+
return { code, message };
|
|
157
|
+
}
|
|
158
|
+
} else {
|
|
159
|
+
if (value === void 0 || value === null || value === "") {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
for (const rule of fieldSchema.rules) {
|
|
164
|
+
if (!rule.validate(value)) {
|
|
165
|
+
const message = resolveMessage(rule.code, formMessages, fieldSchema.messages);
|
|
166
|
+
return { code: rule.code, message };
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// src/validate-form.ts
|
|
173
|
+
function validateForm(values, schema) {
|
|
174
|
+
const errors = {};
|
|
175
|
+
for (const [name, fieldSchema] of Object.entries(schema.fields)) {
|
|
176
|
+
const value = values[name];
|
|
177
|
+
const formMessages = schema.messages?.[name];
|
|
178
|
+
const error = validateField(value, fieldSchema, formMessages);
|
|
179
|
+
if (error) {
|
|
180
|
+
errors[name] = error;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (Object.keys(errors).length > 0) {
|
|
184
|
+
return errors;
|
|
185
|
+
}
|
|
186
|
+
if (schema.resolver) {
|
|
187
|
+
const resolverErrors = schema.resolver(values);
|
|
188
|
+
if (resolverErrors) {
|
|
189
|
+
return { ...errors, ...resolverErrors };
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return errors;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// src/validate-and-create.ts
|
|
196
|
+
function validateAndCreate(value, rules, name) {
|
|
197
|
+
for (const rule of rules) {
|
|
198
|
+
if (!rule.validate(value)) {
|
|
199
|
+
throw new VOValidationError(name, rule.code, value);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return value;
|
|
203
|
+
}
|
|
204
|
+
function safeValidateAndCreate(value, rules) {
|
|
205
|
+
for (const rule of rules) {
|
|
206
|
+
if (!rule.validate(value)) {
|
|
207
|
+
return { success: false, error: { code: rule.code } };
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return { success: true, data: value };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// src/build-output-values.ts
|
|
214
|
+
function buildOutputValues(values, fields) {
|
|
215
|
+
const output = {};
|
|
216
|
+
for (const [name, fieldSchema] of Object.entries(fields)) {
|
|
217
|
+
let value = values[name];
|
|
218
|
+
const isEmpty = value === void 0 || value === null || value === "";
|
|
219
|
+
if (isEmpty) {
|
|
220
|
+
output[name] = void 0;
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
if (fieldSchema.vo) {
|
|
224
|
+
value = fieldSchema.vo.create(value);
|
|
225
|
+
}
|
|
226
|
+
output[name] = value;
|
|
227
|
+
}
|
|
228
|
+
return output;
|
|
229
|
+
}
|
|
230
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
231
|
+
0 && (module.exports = {
|
|
232
|
+
VOValidationError,
|
|
233
|
+
buildOutputValues,
|
|
234
|
+
createField,
|
|
235
|
+
createFormSchema,
|
|
236
|
+
createRule,
|
|
237
|
+
safeValidateAndCreate,
|
|
238
|
+
validateAndCreate,
|
|
239
|
+
validateField,
|
|
240
|
+
validateForm,
|
|
241
|
+
vo
|
|
242
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
type Brand<T, B extends string> = T & {
|
|
2
|
+
readonly __brand: B;
|
|
3
|
+
};
|
|
4
|
+
type ValidationRule<T, C extends string = string> = {
|
|
5
|
+
code: C;
|
|
6
|
+
validate: (value: T) => boolean;
|
|
7
|
+
};
|
|
8
|
+
type CreateResult<T> = {
|
|
9
|
+
success: true;
|
|
10
|
+
data: T;
|
|
11
|
+
} | {
|
|
12
|
+
success: false;
|
|
13
|
+
error: {
|
|
14
|
+
code: string;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
type VODefinition<TInput, TBrand extends string, TCodes extends string = string> = {
|
|
18
|
+
brand: TBrand;
|
|
19
|
+
rules: ValidationRule<TInput, TCodes>[];
|
|
20
|
+
create: (input: TInput) => Brand<TInput, TBrand>;
|
|
21
|
+
safeCreate: (input: TInput) => CreateResult<Brand<TInput, TBrand>>;
|
|
22
|
+
};
|
|
23
|
+
type Infer<D> = D extends VODefinition<infer T, infer B, any> ? Brand<T, B> : D extends {
|
|
24
|
+
create: (input: any) => infer R;
|
|
25
|
+
} ? R : never;
|
|
26
|
+
type ErrorMessageMap<C extends string = string> = {
|
|
27
|
+
[K in C]?: string;
|
|
28
|
+
};
|
|
29
|
+
type ErrorMessageFn<C extends string = string> = (error: {
|
|
30
|
+
code: C;
|
|
31
|
+
}) => string;
|
|
32
|
+
type ErrorMessages<C extends string = string> = ErrorMessageMap<C> | ErrorMessageFn<C>;
|
|
33
|
+
type VOLike<TInput, TOutput, TCodes extends string = string> = {
|
|
34
|
+
rules: ValidationRule<TInput, TCodes>[];
|
|
35
|
+
create: (input: TInput) => TOutput;
|
|
36
|
+
};
|
|
37
|
+
type FieldSchema<T, TOutput, TRequired extends boolean, TCodes extends string = string> = {
|
|
38
|
+
vo: VOLike<T, TOutput, TCodes> | null;
|
|
39
|
+
required: TRequired;
|
|
40
|
+
messages: ErrorMessages<TCodes | (TRequired extends true ? 'REQUIRED' : never)>;
|
|
41
|
+
rules: ValidationRule<T, TCodes>[];
|
|
42
|
+
parse?: (raw: string) => T;
|
|
43
|
+
format?: (value: T) => string;
|
|
44
|
+
};
|
|
45
|
+
type FieldError = {
|
|
46
|
+
code: string;
|
|
47
|
+
message: string;
|
|
48
|
+
};
|
|
49
|
+
type FormErrors = Record<string, FieldError>;
|
|
50
|
+
type FormSchemaConfig<TFields extends Record<string, FieldSchema<any, any, boolean, any>>> = {
|
|
51
|
+
fields: TFields;
|
|
52
|
+
messages?: Record<string, ErrorMessages>;
|
|
53
|
+
resolver?: (values: FormInputValues<TFields>) => FormErrors | null;
|
|
54
|
+
};
|
|
55
|
+
type FormSchema<TFields extends Record<string, FieldSchema<any, any, boolean, any>>> = {
|
|
56
|
+
fields: TFields;
|
|
57
|
+
messages?: Record<string, ErrorMessages>;
|
|
58
|
+
resolver?: (values: FormInputValues<TFields>) => FormErrors | null;
|
|
59
|
+
};
|
|
60
|
+
type FormInputValues<TFields extends Record<string, FieldSchema<any, any, boolean, any>>> = {
|
|
61
|
+
[K in keyof TFields]: TFields[K] extends FieldSchema<infer TInput, any, infer TRequired, any> ? TRequired extends true ? TInput : TInput | undefined : never;
|
|
62
|
+
};
|
|
63
|
+
type FormOutputValues<TFields extends Record<string, FieldSchema<any, any, boolean, any>>> = {
|
|
64
|
+
[K in keyof TFields]: TFields[K] extends FieldSchema<any, infer TOutput, infer TRequired, any> ? TRequired extends true ? TOutput : TOutput | undefined : never;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
declare class VOValidationError extends Error {
|
|
68
|
+
readonly brand: string;
|
|
69
|
+
readonly code: string;
|
|
70
|
+
readonly input: unknown;
|
|
71
|
+
constructor(brand: string, code: string, input: unknown);
|
|
72
|
+
}
|
|
73
|
+
declare function vo<B extends string, T = string, C extends string = string>(brand: B, rules: ValidationRule<T, C>[]): VODefinition<T, B, C>;
|
|
74
|
+
|
|
75
|
+
type RuleCreator<T, O, C extends string = string> = O extends void ? () => ValidationRule<T, C> : (option: O) => ValidationRule<T, C>;
|
|
76
|
+
declare function createRule<T, O = void, C extends string = string>(code: C, validate: (value: T, option: O) => boolean): RuleCreator<T, O, C>;
|
|
77
|
+
|
|
78
|
+
type FieldFactory<TInput, TOutput, TCodes extends string> = {
|
|
79
|
+
(config: {
|
|
80
|
+
required: true;
|
|
81
|
+
messages?: ErrorMessages<TCodes | 'REQUIRED'>;
|
|
82
|
+
}): FieldSchema<TInput, TOutput, true, TCodes>;
|
|
83
|
+
(config?: {
|
|
84
|
+
required?: false;
|
|
85
|
+
messages?: ErrorMessages<TCodes>;
|
|
86
|
+
}): FieldSchema<TInput, TOutput, false, TCodes>;
|
|
87
|
+
};
|
|
88
|
+
declare function createField<TInput, TOutput, TCodes extends string>(vo: VOLike<TInput, TOutput, TCodes>, options?: {
|
|
89
|
+
messages?: ErrorMessages<TCodes>;
|
|
90
|
+
parse?: (raw: string) => TInput;
|
|
91
|
+
format?: (value: TInput) => string;
|
|
92
|
+
}): FieldFactory<TInput, TOutput, TCodes>;
|
|
93
|
+
declare function createField<T, TOut = T, TCodes extends string = string>(config?: {
|
|
94
|
+
rules?: ValidationRule<T, TCodes>[];
|
|
95
|
+
messages?: ErrorMessages<TCodes>;
|
|
96
|
+
parse?: (raw: string) => T;
|
|
97
|
+
format?: (value: T) => string;
|
|
98
|
+
}): FieldFactory<T, TOut, TCodes>;
|
|
99
|
+
|
|
100
|
+
declare function createFormSchema<TFields extends Record<string, FieldSchema<any, any, boolean, any>>>(config: FormSchemaConfig<TFields>): FormSchema<TFields>;
|
|
101
|
+
|
|
102
|
+
declare function validateField<T>(value: T | undefined | null, fieldSchema: FieldSchema<T, any, boolean, any>, formMessages?: ErrorMessages): FieldError | null;
|
|
103
|
+
|
|
104
|
+
declare function validateForm<TFields extends Record<string, FieldSchema<any, any, boolean, any>>>(values: FormInputValues<TFields>, schema: FormSchema<TFields>): FormErrors;
|
|
105
|
+
|
|
106
|
+
declare function validateAndCreate<T>(value: T, rules: readonly {
|
|
107
|
+
code: string;
|
|
108
|
+
validate: (value: T) => boolean;
|
|
109
|
+
}[], name: string): T;
|
|
110
|
+
declare function safeValidateAndCreate<T>(value: T, rules: readonly {
|
|
111
|
+
code: string;
|
|
112
|
+
validate: (value: T) => boolean;
|
|
113
|
+
}[]): CreateResult<T>;
|
|
114
|
+
|
|
115
|
+
declare function buildOutputValues(values: Record<string, unknown>, fields: Record<string, FieldSchema<any, any, boolean, any>>): Record<string, unknown>;
|
|
116
|
+
|
|
117
|
+
export { type Brand, type CreateResult, type ErrorMessageFn, type ErrorMessageMap, type ErrorMessages, type FieldError, type FieldSchema, type FormErrors, type FormInputValues, type FormOutputValues, type FormSchema, type Infer, type VODefinition, type VOLike, VOValidationError, type ValidationRule, buildOutputValues, createField, createFormSchema, createRule, safeValidateAndCreate, validateAndCreate, validateField, validateForm, vo };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
type Brand<T, B extends string> = T & {
|
|
2
|
+
readonly __brand: B;
|
|
3
|
+
};
|
|
4
|
+
type ValidationRule<T, C extends string = string> = {
|
|
5
|
+
code: C;
|
|
6
|
+
validate: (value: T) => boolean;
|
|
7
|
+
};
|
|
8
|
+
type CreateResult<T> = {
|
|
9
|
+
success: true;
|
|
10
|
+
data: T;
|
|
11
|
+
} | {
|
|
12
|
+
success: false;
|
|
13
|
+
error: {
|
|
14
|
+
code: string;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
type VODefinition<TInput, TBrand extends string, TCodes extends string = string> = {
|
|
18
|
+
brand: TBrand;
|
|
19
|
+
rules: ValidationRule<TInput, TCodes>[];
|
|
20
|
+
create: (input: TInput) => Brand<TInput, TBrand>;
|
|
21
|
+
safeCreate: (input: TInput) => CreateResult<Brand<TInput, TBrand>>;
|
|
22
|
+
};
|
|
23
|
+
type Infer<D> = D extends VODefinition<infer T, infer B, any> ? Brand<T, B> : D extends {
|
|
24
|
+
create: (input: any) => infer R;
|
|
25
|
+
} ? R : never;
|
|
26
|
+
type ErrorMessageMap<C extends string = string> = {
|
|
27
|
+
[K in C]?: string;
|
|
28
|
+
};
|
|
29
|
+
type ErrorMessageFn<C extends string = string> = (error: {
|
|
30
|
+
code: C;
|
|
31
|
+
}) => string;
|
|
32
|
+
type ErrorMessages<C extends string = string> = ErrorMessageMap<C> | ErrorMessageFn<C>;
|
|
33
|
+
type VOLike<TInput, TOutput, TCodes extends string = string> = {
|
|
34
|
+
rules: ValidationRule<TInput, TCodes>[];
|
|
35
|
+
create: (input: TInput) => TOutput;
|
|
36
|
+
};
|
|
37
|
+
type FieldSchema<T, TOutput, TRequired extends boolean, TCodes extends string = string> = {
|
|
38
|
+
vo: VOLike<T, TOutput, TCodes> | null;
|
|
39
|
+
required: TRequired;
|
|
40
|
+
messages: ErrorMessages<TCodes | (TRequired extends true ? 'REQUIRED' : never)>;
|
|
41
|
+
rules: ValidationRule<T, TCodes>[];
|
|
42
|
+
parse?: (raw: string) => T;
|
|
43
|
+
format?: (value: T) => string;
|
|
44
|
+
};
|
|
45
|
+
type FieldError = {
|
|
46
|
+
code: string;
|
|
47
|
+
message: string;
|
|
48
|
+
};
|
|
49
|
+
type FormErrors = Record<string, FieldError>;
|
|
50
|
+
type FormSchemaConfig<TFields extends Record<string, FieldSchema<any, any, boolean, any>>> = {
|
|
51
|
+
fields: TFields;
|
|
52
|
+
messages?: Record<string, ErrorMessages>;
|
|
53
|
+
resolver?: (values: FormInputValues<TFields>) => FormErrors | null;
|
|
54
|
+
};
|
|
55
|
+
type FormSchema<TFields extends Record<string, FieldSchema<any, any, boolean, any>>> = {
|
|
56
|
+
fields: TFields;
|
|
57
|
+
messages?: Record<string, ErrorMessages>;
|
|
58
|
+
resolver?: (values: FormInputValues<TFields>) => FormErrors | null;
|
|
59
|
+
};
|
|
60
|
+
type FormInputValues<TFields extends Record<string, FieldSchema<any, any, boolean, any>>> = {
|
|
61
|
+
[K in keyof TFields]: TFields[K] extends FieldSchema<infer TInput, any, infer TRequired, any> ? TRequired extends true ? TInput : TInput | undefined : never;
|
|
62
|
+
};
|
|
63
|
+
type FormOutputValues<TFields extends Record<string, FieldSchema<any, any, boolean, any>>> = {
|
|
64
|
+
[K in keyof TFields]: TFields[K] extends FieldSchema<any, infer TOutput, infer TRequired, any> ? TRequired extends true ? TOutput : TOutput | undefined : never;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
declare class VOValidationError extends Error {
|
|
68
|
+
readonly brand: string;
|
|
69
|
+
readonly code: string;
|
|
70
|
+
readonly input: unknown;
|
|
71
|
+
constructor(brand: string, code: string, input: unknown);
|
|
72
|
+
}
|
|
73
|
+
declare function vo<B extends string, T = string, C extends string = string>(brand: B, rules: ValidationRule<T, C>[]): VODefinition<T, B, C>;
|
|
74
|
+
|
|
75
|
+
type RuleCreator<T, O, C extends string = string> = O extends void ? () => ValidationRule<T, C> : (option: O) => ValidationRule<T, C>;
|
|
76
|
+
declare function createRule<T, O = void, C extends string = string>(code: C, validate: (value: T, option: O) => boolean): RuleCreator<T, O, C>;
|
|
77
|
+
|
|
78
|
+
type FieldFactory<TInput, TOutput, TCodes extends string> = {
|
|
79
|
+
(config: {
|
|
80
|
+
required: true;
|
|
81
|
+
messages?: ErrorMessages<TCodes | 'REQUIRED'>;
|
|
82
|
+
}): FieldSchema<TInput, TOutput, true, TCodes>;
|
|
83
|
+
(config?: {
|
|
84
|
+
required?: false;
|
|
85
|
+
messages?: ErrorMessages<TCodes>;
|
|
86
|
+
}): FieldSchema<TInput, TOutput, false, TCodes>;
|
|
87
|
+
};
|
|
88
|
+
declare function createField<TInput, TOutput, TCodes extends string>(vo: VOLike<TInput, TOutput, TCodes>, options?: {
|
|
89
|
+
messages?: ErrorMessages<TCodes>;
|
|
90
|
+
parse?: (raw: string) => TInput;
|
|
91
|
+
format?: (value: TInput) => string;
|
|
92
|
+
}): FieldFactory<TInput, TOutput, TCodes>;
|
|
93
|
+
declare function createField<T, TOut = T, TCodes extends string = string>(config?: {
|
|
94
|
+
rules?: ValidationRule<T, TCodes>[];
|
|
95
|
+
messages?: ErrorMessages<TCodes>;
|
|
96
|
+
parse?: (raw: string) => T;
|
|
97
|
+
format?: (value: T) => string;
|
|
98
|
+
}): FieldFactory<T, TOut, TCodes>;
|
|
99
|
+
|
|
100
|
+
declare function createFormSchema<TFields extends Record<string, FieldSchema<any, any, boolean, any>>>(config: FormSchemaConfig<TFields>): FormSchema<TFields>;
|
|
101
|
+
|
|
102
|
+
declare function validateField<T>(value: T | undefined | null, fieldSchema: FieldSchema<T, any, boolean, any>, formMessages?: ErrorMessages): FieldError | null;
|
|
103
|
+
|
|
104
|
+
declare function validateForm<TFields extends Record<string, FieldSchema<any, any, boolean, any>>>(values: FormInputValues<TFields>, schema: FormSchema<TFields>): FormErrors;
|
|
105
|
+
|
|
106
|
+
declare function validateAndCreate<T>(value: T, rules: readonly {
|
|
107
|
+
code: string;
|
|
108
|
+
validate: (value: T) => boolean;
|
|
109
|
+
}[], name: string): T;
|
|
110
|
+
declare function safeValidateAndCreate<T>(value: T, rules: readonly {
|
|
111
|
+
code: string;
|
|
112
|
+
validate: (value: T) => boolean;
|
|
113
|
+
}[]): CreateResult<T>;
|
|
114
|
+
|
|
115
|
+
declare function buildOutputValues(values: Record<string, unknown>, fields: Record<string, FieldSchema<any, any, boolean, any>>): Record<string, unknown>;
|
|
116
|
+
|
|
117
|
+
export { type Brand, type CreateResult, type ErrorMessageFn, type ErrorMessageMap, type ErrorMessages, type FieldError, type FieldSchema, type FormErrors, type FormInputValues, type FormOutputValues, type FormSchema, type Infer, type VODefinition, type VOLike, VOValidationError, type ValidationRule, buildOutputValues, createField, createFormSchema, createRule, safeValidateAndCreate, validateAndCreate, validateField, validateForm, vo };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// src/vo.ts
|
|
2
|
+
var VOValidationError = class extends Error {
|
|
3
|
+
constructor(brand, code, input) {
|
|
4
|
+
super(`${brand} is not valid (${code})`);
|
|
5
|
+
this.brand = brand;
|
|
6
|
+
this.code = code;
|
|
7
|
+
this.input = input;
|
|
8
|
+
this.name = "VOValidationError";
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
function vo(brand, rules) {
|
|
12
|
+
return {
|
|
13
|
+
brand,
|
|
14
|
+
rules,
|
|
15
|
+
create(input) {
|
|
16
|
+
for (const rule of rules) {
|
|
17
|
+
if (!rule.validate(input)) {
|
|
18
|
+
throw new VOValidationError(brand, rule.code, input);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return input;
|
|
22
|
+
},
|
|
23
|
+
safeCreate(input) {
|
|
24
|
+
for (const rule of rules) {
|
|
25
|
+
if (!rule.validate(input)) {
|
|
26
|
+
return { success: false, error: { code: rule.code } };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return { success: true, data: input };
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/create-rule.ts
|
|
35
|
+
function createRule(code, validate) {
|
|
36
|
+
return ((option) => ({
|
|
37
|
+
code,
|
|
38
|
+
validate: (value) => validate(value, option)
|
|
39
|
+
}));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/create-field.ts
|
|
43
|
+
function createField(voOrConfig, options) {
|
|
44
|
+
if (voOrConfig && "create" in voOrConfig && typeof voOrConfig.create === "function") {
|
|
45
|
+
let factory2 = function(config2 = {}) {
|
|
46
|
+
return {
|
|
47
|
+
vo: voDef,
|
|
48
|
+
required: config2.required ?? false,
|
|
49
|
+
messages: mergeMessages(definitionMessages2, config2.messages),
|
|
50
|
+
rules: [...voDef.rules],
|
|
51
|
+
parse: parse2,
|
|
52
|
+
format: format2
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
var factory = factory2;
|
|
56
|
+
const voDef = voOrConfig;
|
|
57
|
+
const definitionMessages2 = options?.messages;
|
|
58
|
+
const parse2 = options?.parse;
|
|
59
|
+
const format2 = options?.format;
|
|
60
|
+
return factory2;
|
|
61
|
+
}
|
|
62
|
+
const config = voOrConfig ?? {};
|
|
63
|
+
const definitionMessages = config.messages;
|
|
64
|
+
const definitionRules = config.rules ?? [];
|
|
65
|
+
const parse = config.parse;
|
|
66
|
+
const format = config.format;
|
|
67
|
+
function factory(factoryConfig = {}) {
|
|
68
|
+
return {
|
|
69
|
+
vo: null,
|
|
70
|
+
required: factoryConfig.required ?? false,
|
|
71
|
+
messages: mergeMessages(definitionMessages, factoryConfig.messages),
|
|
72
|
+
rules: [...definitionRules],
|
|
73
|
+
parse,
|
|
74
|
+
format
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return factory;
|
|
78
|
+
}
|
|
79
|
+
function mergeMessages(definition, factory) {
|
|
80
|
+
if (!definition && !factory) return {};
|
|
81
|
+
if (!definition) return factory;
|
|
82
|
+
if (!factory) return definition;
|
|
83
|
+
if (typeof factory === "function") return factory;
|
|
84
|
+
if (typeof definition === "function") return factory;
|
|
85
|
+
return { ...definition, ...factory };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// src/create-form-schema.ts
|
|
89
|
+
function createFormSchema(config) {
|
|
90
|
+
return {
|
|
91
|
+
fields: config.fields,
|
|
92
|
+
messages: config.messages,
|
|
93
|
+
resolver: config.resolver
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// src/resolve-message.ts
|
|
98
|
+
var DEFAULT_MESSAGES = {
|
|
99
|
+
REQUIRED: "This field is required"
|
|
100
|
+
};
|
|
101
|
+
function resolveMessage(code, ...messageSources) {
|
|
102
|
+
for (const source of messageSources) {
|
|
103
|
+
if (!source) continue;
|
|
104
|
+
if (typeof source === "function") {
|
|
105
|
+
const result = source({ code });
|
|
106
|
+
if (result) return result;
|
|
107
|
+
} else {
|
|
108
|
+
const msg = source[code];
|
|
109
|
+
if (msg) return msg;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return DEFAULT_MESSAGES[code] ?? code;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// src/validate-field.ts
|
|
116
|
+
function validateField(value, fieldSchema, formMessages) {
|
|
117
|
+
if (fieldSchema.required) {
|
|
118
|
+
if (value === void 0 || value === null || value === "") {
|
|
119
|
+
const code = "REQUIRED";
|
|
120
|
+
const message = resolveMessage(code, formMessages, fieldSchema.messages);
|
|
121
|
+
return { code, message };
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
if (value === void 0 || value === null || value === "") {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
for (const rule of fieldSchema.rules) {
|
|
129
|
+
if (!rule.validate(value)) {
|
|
130
|
+
const message = resolveMessage(rule.code, formMessages, fieldSchema.messages);
|
|
131
|
+
return { code: rule.code, message };
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// src/validate-form.ts
|
|
138
|
+
function validateForm(values, schema) {
|
|
139
|
+
const errors = {};
|
|
140
|
+
for (const [name, fieldSchema] of Object.entries(schema.fields)) {
|
|
141
|
+
const value = values[name];
|
|
142
|
+
const formMessages = schema.messages?.[name];
|
|
143
|
+
const error = validateField(value, fieldSchema, formMessages);
|
|
144
|
+
if (error) {
|
|
145
|
+
errors[name] = error;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (Object.keys(errors).length > 0) {
|
|
149
|
+
return errors;
|
|
150
|
+
}
|
|
151
|
+
if (schema.resolver) {
|
|
152
|
+
const resolverErrors = schema.resolver(values);
|
|
153
|
+
if (resolverErrors) {
|
|
154
|
+
return { ...errors, ...resolverErrors };
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return errors;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// src/validate-and-create.ts
|
|
161
|
+
function validateAndCreate(value, rules, name) {
|
|
162
|
+
for (const rule of rules) {
|
|
163
|
+
if (!rule.validate(value)) {
|
|
164
|
+
throw new VOValidationError(name, rule.code, value);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return value;
|
|
168
|
+
}
|
|
169
|
+
function safeValidateAndCreate(value, rules) {
|
|
170
|
+
for (const rule of rules) {
|
|
171
|
+
if (!rule.validate(value)) {
|
|
172
|
+
return { success: false, error: { code: rule.code } };
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return { success: true, data: value };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// src/build-output-values.ts
|
|
179
|
+
function buildOutputValues(values, fields) {
|
|
180
|
+
const output = {};
|
|
181
|
+
for (const [name, fieldSchema] of Object.entries(fields)) {
|
|
182
|
+
let value = values[name];
|
|
183
|
+
const isEmpty = value === void 0 || value === null || value === "";
|
|
184
|
+
if (isEmpty) {
|
|
185
|
+
output[name] = void 0;
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (fieldSchema.vo) {
|
|
189
|
+
value = fieldSchema.vo.create(value);
|
|
190
|
+
}
|
|
191
|
+
output[name] = value;
|
|
192
|
+
}
|
|
193
|
+
return output;
|
|
194
|
+
}
|
|
195
|
+
export {
|
|
196
|
+
VOValidationError,
|
|
197
|
+
buildOutputValues,
|
|
198
|
+
createField,
|
|
199
|
+
createFormSchema,
|
|
200
|
+
createRule,
|
|
201
|
+
safeValidateAndCreate,
|
|
202
|
+
validateAndCreate,
|
|
203
|
+
validateField,
|
|
204
|
+
validateForm,
|
|
205
|
+
vo
|
|
206
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gunubin/vorm-core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "VO-first form validation core — branded types, field schemas, and validation logic",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"form",
|
|
7
|
+
"validation",
|
|
8
|
+
"value-object",
|
|
9
|
+
"branded-types",
|
|
10
|
+
"typescript"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "gunubin",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/gunubin/vorm.git",
|
|
17
|
+
"directory": "packages/core"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/gunubin/vorm#readme",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/gunubin/vorm/issues"
|
|
22
|
+
},
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./dist/index.cjs",
|
|
26
|
+
"module": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.js",
|
|
32
|
+
"require": "./dist/index.cjs"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist"
|
|
37
|
+
],
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"typescript": "^5.9.3",
|
|
43
|
+
"vitest": "^3.0.5"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsup",
|
|
47
|
+
"typecheck": "tsc --noEmit",
|
|
48
|
+
"test": "vitest run",
|
|
49
|
+
"test:watch": "vitest"
|
|
50
|
+
}
|
|
51
|
+
}
|