@gunubin/vorm-form 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/dist/index.cjs +175 -0
- package/dist/index.d.cts +72 -0
- package/dist/index.d.ts +72 -0
- package/dist/index.js +143 -0
- package/package.json +53 -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/dist/index.cjs
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
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
|
+
buildOutputValues: () => buildOutputValues,
|
|
24
|
+
createField: () => createField,
|
|
25
|
+
createFormSchema: () => createFormSchema,
|
|
26
|
+
resolveMessage: () => resolveMessage,
|
|
27
|
+
validateField: () => validateField,
|
|
28
|
+
validateForm: () => validateForm
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(index_exports);
|
|
31
|
+
|
|
32
|
+
// src/create-field.ts
|
|
33
|
+
function createField(voOrConfig, options) {
|
|
34
|
+
if (voOrConfig && "create" in voOrConfig && typeof voOrConfig.create === "function") {
|
|
35
|
+
let factory2 = function(config2 = {}) {
|
|
36
|
+
return {
|
|
37
|
+
vo: voDef,
|
|
38
|
+
required: config2.required ?? false,
|
|
39
|
+
messages: mergeMessages(definitionMessages2, config2.messages),
|
|
40
|
+
rules: [...voDef.rules],
|
|
41
|
+
parse: parse2,
|
|
42
|
+
format: format2
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
var factory = factory2;
|
|
46
|
+
const voDef = voOrConfig;
|
|
47
|
+
const definitionMessages2 = options?.messages;
|
|
48
|
+
const parse2 = options?.parse;
|
|
49
|
+
const format2 = options?.format;
|
|
50
|
+
return factory2;
|
|
51
|
+
}
|
|
52
|
+
const config = voOrConfig ?? {};
|
|
53
|
+
const definitionMessages = config.messages;
|
|
54
|
+
const definitionRules = config.rules ?? [];
|
|
55
|
+
const parse = config.parse;
|
|
56
|
+
const format = config.format;
|
|
57
|
+
function factory(factoryConfig = {}) {
|
|
58
|
+
return {
|
|
59
|
+
vo: null,
|
|
60
|
+
required: factoryConfig.required ?? false,
|
|
61
|
+
messages: mergeMessages(definitionMessages, factoryConfig.messages),
|
|
62
|
+
rules: [...definitionRules],
|
|
63
|
+
parse,
|
|
64
|
+
format
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return factory;
|
|
68
|
+
}
|
|
69
|
+
function mergeMessages(definition, factory) {
|
|
70
|
+
if (!definition && !factory) return {};
|
|
71
|
+
if (!definition) return factory;
|
|
72
|
+
if (!factory) return definition;
|
|
73
|
+
if (typeof factory === "function") return factory;
|
|
74
|
+
if (typeof definition === "function") return factory;
|
|
75
|
+
return { ...definition, ...factory };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// src/create-form-schema.ts
|
|
79
|
+
function createFormSchema(config) {
|
|
80
|
+
return {
|
|
81
|
+
fields: config.fields,
|
|
82
|
+
messages: config.messages,
|
|
83
|
+
resolver: config.resolver
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// src/resolve-message.ts
|
|
88
|
+
var DEFAULT_MESSAGES = {
|
|
89
|
+
REQUIRED: "This field is required"
|
|
90
|
+
};
|
|
91
|
+
function resolveMessage(code, ...messageSources) {
|
|
92
|
+
for (const source of messageSources) {
|
|
93
|
+
if (!source) continue;
|
|
94
|
+
if (typeof source === "function") {
|
|
95
|
+
const result = source({ code });
|
|
96
|
+
if (result) return result;
|
|
97
|
+
} else {
|
|
98
|
+
const msg = source[code];
|
|
99
|
+
if (msg) return msg;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return DEFAULT_MESSAGES[code] ?? code;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/validate-field.ts
|
|
106
|
+
function validateField(value, fieldSchema, formMessages) {
|
|
107
|
+
if (fieldSchema.required) {
|
|
108
|
+
if (value === void 0 || value === null || value === "") {
|
|
109
|
+
const code = "REQUIRED";
|
|
110
|
+
const message = resolveMessage(code, formMessages, fieldSchema.messages);
|
|
111
|
+
return { code, message };
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
if (value === void 0 || value === null || value === "") {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
for (const rule of fieldSchema.rules) {
|
|
119
|
+
if (!rule.validate(value)) {
|
|
120
|
+
const message = resolveMessage(rule.code, formMessages, fieldSchema.messages);
|
|
121
|
+
return { code: rule.code, message };
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/validate-form.ts
|
|
128
|
+
function validateForm(values, schema) {
|
|
129
|
+
const errors = {};
|
|
130
|
+
for (const [name, fieldSchema] of Object.entries(schema.fields)) {
|
|
131
|
+
const value = values[name];
|
|
132
|
+
const formMessages = schema.messages?.[name];
|
|
133
|
+
const error = validateField(value, fieldSchema, formMessages);
|
|
134
|
+
if (error) {
|
|
135
|
+
errors[name] = error;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (Object.keys(errors).length > 0) {
|
|
139
|
+
return errors;
|
|
140
|
+
}
|
|
141
|
+
if (schema.resolver) {
|
|
142
|
+
const resolverErrors = schema.resolver(values);
|
|
143
|
+
if (resolverErrors) {
|
|
144
|
+
return { ...errors, ...resolverErrors };
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return errors;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// src/build-output-values.ts
|
|
151
|
+
function buildOutputValues(values, fields) {
|
|
152
|
+
const output = {};
|
|
153
|
+
for (const [name, fieldSchema] of Object.entries(fields)) {
|
|
154
|
+
let value = values[name];
|
|
155
|
+
const isEmpty = value === void 0 || value === null || value === "";
|
|
156
|
+
if (isEmpty) {
|
|
157
|
+
output[name] = void 0;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (fieldSchema.vo) {
|
|
161
|
+
value = fieldSchema.vo.create(value);
|
|
162
|
+
}
|
|
163
|
+
output[name] = value;
|
|
164
|
+
}
|
|
165
|
+
return output;
|
|
166
|
+
}
|
|
167
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
168
|
+
0 && (module.exports = {
|
|
169
|
+
buildOutputValues,
|
|
170
|
+
createField,
|
|
171
|
+
createFormSchema,
|
|
172
|
+
resolveMessage,
|
|
173
|
+
validateField,
|
|
174
|
+
validateForm
|
|
175
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { VOLike, ValidationRule } from '@gunubin/vorm-core';
|
|
2
|
+
|
|
3
|
+
type ErrorMessageMap<C extends string = string> = {
|
|
4
|
+
[K in C]?: string;
|
|
5
|
+
};
|
|
6
|
+
type ErrorMessageFn<C extends string = string> = (error: {
|
|
7
|
+
code: C;
|
|
8
|
+
}) => string;
|
|
9
|
+
type ErrorMessages<C extends string = string> = ErrorMessageMap<C> | ErrorMessageFn<C>;
|
|
10
|
+
type FieldSchema<T, TOutput, TRequired extends boolean, TCodes extends string = string> = {
|
|
11
|
+
vo: VOLike<T, TOutput, TCodes> | null;
|
|
12
|
+
required: TRequired;
|
|
13
|
+
messages: ErrorMessages<TCodes | (TRequired extends true ? 'REQUIRED' : never)>;
|
|
14
|
+
rules: ValidationRule<T, TCodes>[];
|
|
15
|
+
parse?: (raw: string) => T;
|
|
16
|
+
format?: (value: T) => string;
|
|
17
|
+
};
|
|
18
|
+
type FieldError = {
|
|
19
|
+
code: string;
|
|
20
|
+
message: string;
|
|
21
|
+
};
|
|
22
|
+
type FormErrors = Record<string, FieldError>;
|
|
23
|
+
type FormSchemaConfig<TFields extends Record<string, FieldSchema<any, any, boolean, any>>> = {
|
|
24
|
+
fields: TFields;
|
|
25
|
+
messages?: Record<string, ErrorMessages>;
|
|
26
|
+
resolver?: (values: FormInputValues<TFields>) => FormErrors | null;
|
|
27
|
+
};
|
|
28
|
+
type FormSchema<TFields extends Record<string, FieldSchema<any, any, boolean, any>>> = {
|
|
29
|
+
fields: TFields;
|
|
30
|
+
messages?: Record<string, ErrorMessages>;
|
|
31
|
+
resolver?: (values: FormInputValues<TFields>) => FormErrors | null;
|
|
32
|
+
};
|
|
33
|
+
type FormInputValues<TFields extends Record<string, FieldSchema<any, any, boolean, any>>> = {
|
|
34
|
+
[K in keyof TFields]: TFields[K] extends FieldSchema<infer TInput, any, infer TRequired, any> ? TRequired extends true ? TInput : TInput | undefined : never;
|
|
35
|
+
};
|
|
36
|
+
type FormOutputValues<TFields extends Record<string, FieldSchema<any, any, boolean, any>>> = {
|
|
37
|
+
[K in keyof TFields]: TFields[K] extends FieldSchema<any, infer TOutput, infer TRequired, any> ? TRequired extends true ? TOutput : TOutput | undefined : never;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
type FieldFactory<TInput, TOutput, TCodes extends string> = {
|
|
41
|
+
(config: {
|
|
42
|
+
required: true;
|
|
43
|
+
messages?: ErrorMessages<TCodes | 'REQUIRED'>;
|
|
44
|
+
}): FieldSchema<TInput, TOutput, true, TCodes>;
|
|
45
|
+
(config?: {
|
|
46
|
+
required?: false;
|
|
47
|
+
messages?: ErrorMessages<TCodes>;
|
|
48
|
+
}): FieldSchema<TInput, TOutput, false, TCodes>;
|
|
49
|
+
};
|
|
50
|
+
declare function createField<TInput, TOutput, TCodes extends string>(vo: VOLike<TInput, TOutput, TCodes>, options?: {
|
|
51
|
+
messages?: ErrorMessages<TCodes>;
|
|
52
|
+
parse?: (raw: string) => TInput;
|
|
53
|
+
format?: (value: TInput) => string;
|
|
54
|
+
}): FieldFactory<TInput, TOutput, TCodes>;
|
|
55
|
+
declare function createField<T, TOut = T, TCodes extends string = string>(config?: {
|
|
56
|
+
rules?: ValidationRule<T, TCodes>[];
|
|
57
|
+
messages?: ErrorMessages<TCodes>;
|
|
58
|
+
parse?: (raw: string) => T;
|
|
59
|
+
format?: (value: T) => string;
|
|
60
|
+
}): FieldFactory<T, TOut, TCodes>;
|
|
61
|
+
|
|
62
|
+
declare function createFormSchema<TFields extends Record<string, FieldSchema<any, any, boolean, any>>>(config: FormSchemaConfig<TFields>): FormSchema<TFields>;
|
|
63
|
+
|
|
64
|
+
declare function validateField<T>(value: T | undefined | null, fieldSchema: FieldSchema<T, any, boolean, any>, formMessages?: ErrorMessages): FieldError | null;
|
|
65
|
+
|
|
66
|
+
declare function validateForm<TFields extends Record<string, FieldSchema<any, any, boolean, any>>>(values: FormInputValues<TFields>, schema: FormSchema<TFields>): FormErrors;
|
|
67
|
+
|
|
68
|
+
declare function buildOutputValues(values: Record<string, unknown>, fields: Record<string, FieldSchema<any, any, boolean, any>>): Record<string, unknown>;
|
|
69
|
+
|
|
70
|
+
declare function resolveMessage(code: string, ...messageSources: (ErrorMessages | undefined)[]): string;
|
|
71
|
+
|
|
72
|
+
export { type ErrorMessageFn, type ErrorMessageMap, type ErrorMessages, type FieldError, type FieldSchema, type FormErrors, type FormInputValues, type FormOutputValues, type FormSchema, type FormSchemaConfig, buildOutputValues, createField, createFormSchema, resolveMessage, validateField, validateForm };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { VOLike, ValidationRule } from '@gunubin/vorm-core';
|
|
2
|
+
|
|
3
|
+
type ErrorMessageMap<C extends string = string> = {
|
|
4
|
+
[K in C]?: string;
|
|
5
|
+
};
|
|
6
|
+
type ErrorMessageFn<C extends string = string> = (error: {
|
|
7
|
+
code: C;
|
|
8
|
+
}) => string;
|
|
9
|
+
type ErrorMessages<C extends string = string> = ErrorMessageMap<C> | ErrorMessageFn<C>;
|
|
10
|
+
type FieldSchema<T, TOutput, TRequired extends boolean, TCodes extends string = string> = {
|
|
11
|
+
vo: VOLike<T, TOutput, TCodes> | null;
|
|
12
|
+
required: TRequired;
|
|
13
|
+
messages: ErrorMessages<TCodes | (TRequired extends true ? 'REQUIRED' : never)>;
|
|
14
|
+
rules: ValidationRule<T, TCodes>[];
|
|
15
|
+
parse?: (raw: string) => T;
|
|
16
|
+
format?: (value: T) => string;
|
|
17
|
+
};
|
|
18
|
+
type FieldError = {
|
|
19
|
+
code: string;
|
|
20
|
+
message: string;
|
|
21
|
+
};
|
|
22
|
+
type FormErrors = Record<string, FieldError>;
|
|
23
|
+
type FormSchemaConfig<TFields extends Record<string, FieldSchema<any, any, boolean, any>>> = {
|
|
24
|
+
fields: TFields;
|
|
25
|
+
messages?: Record<string, ErrorMessages>;
|
|
26
|
+
resolver?: (values: FormInputValues<TFields>) => FormErrors | null;
|
|
27
|
+
};
|
|
28
|
+
type FormSchema<TFields extends Record<string, FieldSchema<any, any, boolean, any>>> = {
|
|
29
|
+
fields: TFields;
|
|
30
|
+
messages?: Record<string, ErrorMessages>;
|
|
31
|
+
resolver?: (values: FormInputValues<TFields>) => FormErrors | null;
|
|
32
|
+
};
|
|
33
|
+
type FormInputValues<TFields extends Record<string, FieldSchema<any, any, boolean, any>>> = {
|
|
34
|
+
[K in keyof TFields]: TFields[K] extends FieldSchema<infer TInput, any, infer TRequired, any> ? TRequired extends true ? TInput : TInput | undefined : never;
|
|
35
|
+
};
|
|
36
|
+
type FormOutputValues<TFields extends Record<string, FieldSchema<any, any, boolean, any>>> = {
|
|
37
|
+
[K in keyof TFields]: TFields[K] extends FieldSchema<any, infer TOutput, infer TRequired, any> ? TRequired extends true ? TOutput : TOutput | undefined : never;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
type FieldFactory<TInput, TOutput, TCodes extends string> = {
|
|
41
|
+
(config: {
|
|
42
|
+
required: true;
|
|
43
|
+
messages?: ErrorMessages<TCodes | 'REQUIRED'>;
|
|
44
|
+
}): FieldSchema<TInput, TOutput, true, TCodes>;
|
|
45
|
+
(config?: {
|
|
46
|
+
required?: false;
|
|
47
|
+
messages?: ErrorMessages<TCodes>;
|
|
48
|
+
}): FieldSchema<TInput, TOutput, false, TCodes>;
|
|
49
|
+
};
|
|
50
|
+
declare function createField<TInput, TOutput, TCodes extends string>(vo: VOLike<TInput, TOutput, TCodes>, options?: {
|
|
51
|
+
messages?: ErrorMessages<TCodes>;
|
|
52
|
+
parse?: (raw: string) => TInput;
|
|
53
|
+
format?: (value: TInput) => string;
|
|
54
|
+
}): FieldFactory<TInput, TOutput, TCodes>;
|
|
55
|
+
declare function createField<T, TOut = T, TCodes extends string = string>(config?: {
|
|
56
|
+
rules?: ValidationRule<T, TCodes>[];
|
|
57
|
+
messages?: ErrorMessages<TCodes>;
|
|
58
|
+
parse?: (raw: string) => T;
|
|
59
|
+
format?: (value: T) => string;
|
|
60
|
+
}): FieldFactory<T, TOut, TCodes>;
|
|
61
|
+
|
|
62
|
+
declare function createFormSchema<TFields extends Record<string, FieldSchema<any, any, boolean, any>>>(config: FormSchemaConfig<TFields>): FormSchema<TFields>;
|
|
63
|
+
|
|
64
|
+
declare function validateField<T>(value: T | undefined | null, fieldSchema: FieldSchema<T, any, boolean, any>, formMessages?: ErrorMessages): FieldError | null;
|
|
65
|
+
|
|
66
|
+
declare function validateForm<TFields extends Record<string, FieldSchema<any, any, boolean, any>>>(values: FormInputValues<TFields>, schema: FormSchema<TFields>): FormErrors;
|
|
67
|
+
|
|
68
|
+
declare function buildOutputValues(values: Record<string, unknown>, fields: Record<string, FieldSchema<any, any, boolean, any>>): Record<string, unknown>;
|
|
69
|
+
|
|
70
|
+
declare function resolveMessage(code: string, ...messageSources: (ErrorMessages | undefined)[]): string;
|
|
71
|
+
|
|
72
|
+
export { type ErrorMessageFn, type ErrorMessageMap, type ErrorMessages, type FieldError, type FieldSchema, type FormErrors, type FormInputValues, type FormOutputValues, type FormSchema, type FormSchemaConfig, buildOutputValues, createField, createFormSchema, resolveMessage, validateField, validateForm };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// src/create-field.ts
|
|
2
|
+
function createField(voOrConfig, options) {
|
|
3
|
+
if (voOrConfig && "create" in voOrConfig && typeof voOrConfig.create === "function") {
|
|
4
|
+
let factory2 = function(config2 = {}) {
|
|
5
|
+
return {
|
|
6
|
+
vo: voDef,
|
|
7
|
+
required: config2.required ?? false,
|
|
8
|
+
messages: mergeMessages(definitionMessages2, config2.messages),
|
|
9
|
+
rules: [...voDef.rules],
|
|
10
|
+
parse: parse2,
|
|
11
|
+
format: format2
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
var factory = factory2;
|
|
15
|
+
const voDef = voOrConfig;
|
|
16
|
+
const definitionMessages2 = options?.messages;
|
|
17
|
+
const parse2 = options?.parse;
|
|
18
|
+
const format2 = options?.format;
|
|
19
|
+
return factory2;
|
|
20
|
+
}
|
|
21
|
+
const config = voOrConfig ?? {};
|
|
22
|
+
const definitionMessages = config.messages;
|
|
23
|
+
const definitionRules = config.rules ?? [];
|
|
24
|
+
const parse = config.parse;
|
|
25
|
+
const format = config.format;
|
|
26
|
+
function factory(factoryConfig = {}) {
|
|
27
|
+
return {
|
|
28
|
+
vo: null,
|
|
29
|
+
required: factoryConfig.required ?? false,
|
|
30
|
+
messages: mergeMessages(definitionMessages, factoryConfig.messages),
|
|
31
|
+
rules: [...definitionRules],
|
|
32
|
+
parse,
|
|
33
|
+
format
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return factory;
|
|
37
|
+
}
|
|
38
|
+
function mergeMessages(definition, factory) {
|
|
39
|
+
if (!definition && !factory) return {};
|
|
40
|
+
if (!definition) return factory;
|
|
41
|
+
if (!factory) return definition;
|
|
42
|
+
if (typeof factory === "function") return factory;
|
|
43
|
+
if (typeof definition === "function") return factory;
|
|
44
|
+
return { ...definition, ...factory };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/create-form-schema.ts
|
|
48
|
+
function createFormSchema(config) {
|
|
49
|
+
return {
|
|
50
|
+
fields: config.fields,
|
|
51
|
+
messages: config.messages,
|
|
52
|
+
resolver: config.resolver
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/resolve-message.ts
|
|
57
|
+
var DEFAULT_MESSAGES = {
|
|
58
|
+
REQUIRED: "This field is required"
|
|
59
|
+
};
|
|
60
|
+
function resolveMessage(code, ...messageSources) {
|
|
61
|
+
for (const source of messageSources) {
|
|
62
|
+
if (!source) continue;
|
|
63
|
+
if (typeof source === "function") {
|
|
64
|
+
const result = source({ code });
|
|
65
|
+
if (result) return result;
|
|
66
|
+
} else {
|
|
67
|
+
const msg = source[code];
|
|
68
|
+
if (msg) return msg;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return DEFAULT_MESSAGES[code] ?? code;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/validate-field.ts
|
|
75
|
+
function validateField(value, fieldSchema, formMessages) {
|
|
76
|
+
if (fieldSchema.required) {
|
|
77
|
+
if (value === void 0 || value === null || value === "") {
|
|
78
|
+
const code = "REQUIRED";
|
|
79
|
+
const message = resolveMessage(code, formMessages, fieldSchema.messages);
|
|
80
|
+
return { code, message };
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
if (value === void 0 || value === null || value === "") {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
for (const rule of fieldSchema.rules) {
|
|
88
|
+
if (!rule.validate(value)) {
|
|
89
|
+
const message = resolveMessage(rule.code, formMessages, fieldSchema.messages);
|
|
90
|
+
return { code: rule.code, message };
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// src/validate-form.ts
|
|
97
|
+
function validateForm(values, schema) {
|
|
98
|
+
const errors = {};
|
|
99
|
+
for (const [name, fieldSchema] of Object.entries(schema.fields)) {
|
|
100
|
+
const value = values[name];
|
|
101
|
+
const formMessages = schema.messages?.[name];
|
|
102
|
+
const error = validateField(value, fieldSchema, formMessages);
|
|
103
|
+
if (error) {
|
|
104
|
+
errors[name] = error;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (Object.keys(errors).length > 0) {
|
|
108
|
+
return errors;
|
|
109
|
+
}
|
|
110
|
+
if (schema.resolver) {
|
|
111
|
+
const resolverErrors = schema.resolver(values);
|
|
112
|
+
if (resolverErrors) {
|
|
113
|
+
return { ...errors, ...resolverErrors };
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return errors;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// src/build-output-values.ts
|
|
120
|
+
function buildOutputValues(values, fields) {
|
|
121
|
+
const output = {};
|
|
122
|
+
for (const [name, fieldSchema] of Object.entries(fields)) {
|
|
123
|
+
let value = values[name];
|
|
124
|
+
const isEmpty = value === void 0 || value === null || value === "";
|
|
125
|
+
if (isEmpty) {
|
|
126
|
+
output[name] = void 0;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (fieldSchema.vo) {
|
|
130
|
+
value = fieldSchema.vo.create(value);
|
|
131
|
+
}
|
|
132
|
+
output[name] = value;
|
|
133
|
+
}
|
|
134
|
+
return output;
|
|
135
|
+
}
|
|
136
|
+
export {
|
|
137
|
+
buildOutputValues,
|
|
138
|
+
createField,
|
|
139
|
+
createFormSchema,
|
|
140
|
+
resolveMessage,
|
|
141
|
+
validateField,
|
|
142
|
+
validateForm
|
|
143
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gunubin/vorm-form",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Field schemas, form validation, and output building for vorm",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"form",
|
|
7
|
+
"validation",
|
|
8
|
+
"field-schema",
|
|
9
|
+
"typescript"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "gunubin",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/gunubin/vorm.git",
|
|
16
|
+
"directory": "packages/form"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/gunubin/vorm#readme",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/gunubin/vorm/issues"
|
|
21
|
+
},
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./dist/index.cjs",
|
|
25
|
+
"module": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.js",
|
|
31
|
+
"require": "./dist/index.cjs"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist"
|
|
36
|
+
],
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@gunubin/vorm-core": "0.2.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"typescript": "^5.9.3",
|
|
45
|
+
"vitest": "^3.0.5"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsup",
|
|
49
|
+
"typecheck": "tsc --noEmit",
|
|
50
|
+
"test": "vitest run",
|
|
51
|
+
"test:watch": "vitest"
|
|
52
|
+
}
|
|
53
|
+
}
|