@formvk/validator 0.1.0 → 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/src/template.ts DELETED
@@ -1,18 +0,0 @@
1
- import { FormPath, isFn, isStr } from '@formvk/shared'
2
-
3
- import { getValidateMessageTemplateEngine } from './registry'
4
- import type { IValidateResult, IValidatorRules } from './types'
5
-
6
- export function render(result: IValidateResult, rules: IValidatorRules): IValidateResult {
7
- const { message } = result
8
- if (isStr(message)) {
9
- const template = getValidateMessageTemplateEngine()
10
- if (isFn(template)) {
11
- result.message = template(message, rules)
12
- }
13
- result.message = result.message.replace(/\{\{\s*([\w.]+)\s*\}\}/g, (_, $0) => {
14
- return FormPath.getIn(rules, $0)
15
- })
16
- }
17
- return result
18
- }
package/src/types.ts DELETED
@@ -1,105 +0,0 @@
1
- export type ValidatorFormats =
2
- | 'url'
3
- | 'email'
4
- | 'ipv6'
5
- | 'ipv4'
6
- | 'number'
7
- | 'integer'
8
- | 'idcard'
9
- | 'qq'
10
- | 'phone'
11
- | 'money'
12
- | 'zh'
13
- | 'date'
14
- | 'zip'
15
- | (string & {})
16
-
17
- export interface IValidateResult {
18
- type: 'error' | 'warning' | 'success' | (string & {})
19
- message: string
20
- }
21
-
22
- export interface IValidateResults {
23
- error?: string[]
24
- warning?: string[]
25
- success?: string[]
26
- }
27
-
28
- export function isValidateResult(obj: any): obj is IValidateResult {
29
- return !!obj['type'] && !!obj['message']
30
- }
31
-
32
- export type ValidatorFunctionResponse = string | boolean | IValidateResult | undefined | null
33
-
34
- export type ValidatorFunction<Context = any> = (
35
- value: any,
36
- rule: IValidatorRules<Context>,
37
- ctx: Context,
38
- render: (message: string, scope?: any) => string
39
- ) => ValidatorFunctionResponse | Promise<ValidatorFunctionResponse>
40
-
41
- export type ValidatorParsedFunction<Context = any> = (
42
- value: any,
43
- ctx?: Context
44
- ) => IValidateResult | Promise<IValidateResult> | null
45
-
46
- export type ValidatorTriggerType = 'onInput' | 'onFocus' | 'onBlur' | (string & {})
47
-
48
- export interface IValidatorRules<Context = any> {
49
- triggerType?: ValidatorTriggerType
50
- format?: ValidatorFormats
51
- validator?: ValidatorFunction<Context>
52
- required?: boolean
53
- pattern?: RegExp | string
54
- max?: number
55
- maximum?: number
56
- maxItems?: number
57
- minItems?: number
58
- maxLength?: number
59
- minLength?: number
60
- exclusiveMaximum?: number
61
- exclusiveMinimum?: number
62
- minimum?: number
63
- min?: number
64
- len?: number
65
- whitespace?: boolean
66
- enum?: any[]
67
- const?: any
68
- multipleOf?: number
69
- uniqueItems?: boolean
70
- maxProperties?: number
71
- minProperties?: number
72
- message?: string
73
- [key: string]: any
74
- }
75
-
76
- export interface IRegistryLocaleMessages {
77
- [key: string]: string | IRegistryLocaleMessages
78
- }
79
-
80
- export interface IRegistryLocales {
81
- [language: string]: IRegistryLocaleMessages
82
- }
83
-
84
- export interface IRegistryRules<Context = any> {
85
- [key: string]: ValidatorFunction<Context>
86
- }
87
-
88
- export interface IRegistryFormats {
89
- [key: string]: string | RegExp
90
- }
91
-
92
- export type ValidatorDescription<Context = any> =
93
- | ValidatorFormats
94
- | ValidatorFunction<Context>
95
- | IValidatorRules<Context>
96
-
97
- export type MultiValidator<Context = any> = ValidatorDescription<Context>[]
98
-
99
- export type Validator<Context = any> = ValidatorDescription<Context> | MultiValidator<Context>
100
-
101
- export interface IValidatorOptions<Context = any> {
102
- validateFirst?: boolean
103
- triggerType?: ValidatorTriggerType
104
- context?: Context
105
- }
package/src/validator.ts DELETED
@@ -1,36 +0,0 @@
1
- import formats from './formats'
2
- import locales from './locale'
3
- import { parseValidator } from './parser'
4
- import { registerValidateFormats, registerValidateLocale, registerValidateRules } from './registry'
5
- import rules from './rules'
6
- import type { IValidateResults, IValidatorOptions, Validator } from './types'
7
-
8
- registerValidateRules(rules)
9
-
10
- registerValidateLocale(locales)
11
-
12
- registerValidateFormats(formats)
13
-
14
- export async function validate<Context = any>(
15
- value: any,
16
- validator?: Validator<Context>,
17
- options?: IValidatorOptions<Context>
18
- ): Promise<IValidateResults> {
19
- const validates = parseValidator(validator, options)
20
- const results: Record<string, string[]> = {
21
- error: [],
22
- success: [],
23
- warning: [],
24
- }
25
- for (let i = 0; i < validates.length; i++) {
26
- const result = await validates[i](value, options?.context)
27
- if (!result) continue
28
- const { type, message } = result
29
- results[type] = results[type] || []
30
- if (message) {
31
- results[type].push(message)
32
- if (options?.validateFirst) break
33
- }
34
- }
35
- return results
36
- }
@@ -1,8 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "paths": {},
5
- "types": []
6
- },
7
- "include": ["./src/**/*.ts"]
8
- }
package/tsconfig.json DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "include": ["./src/**/*.ts", "./test/**/*.ts"]
4
- }
package/vite.config.ts DELETED
@@ -1,18 +0,0 @@
1
- import { defineConfig } from 'vite'
2
- import dts from 'vite-plugin-dts'
3
- export default defineConfig({
4
- plugins: [dts({ tsconfigPath: './tsconfig.build.json', entryRoot: 'src', bundleTypes: true })],
5
- build: {
6
- lib: {
7
- entry: 'src/index.ts',
8
- name: 'Formvk.Validator',
9
- fileName: 'index',
10
- formats: ['es'],
11
- },
12
- rolldownOptions: {
13
- external: ['@formvk/shared'],
14
- },
15
- sourcemap: true,
16
- target: 'esnext',
17
- },
18
- })