|
| 1 | +/* eslint-disable consistent-return */ |
| 2 | +import type { AllPathsKeys } from 'skyroc-type-utils'; |
| 3 | + |
| 4 | +import { keyOfName } from '../../utils/util'; |
| 5 | +import type { Middleware } from '../middleware'; |
| 6 | + |
| 7 | +import { dispatchIssues } from './utils'; |
| 8 | + |
| 9 | +/** |
| 10 | + * The Standard Schema interface. |
| 11 | + */ |
| 12 | +export type StandardSchemaV1<Input = unknown, Output = Input> = { |
| 13 | + /** |
| 14 | + * The Standard Schema properties. |
| 15 | + */ |
| 16 | + readonly '~standard': StandardSchemaV1Props<Input, Output>; |
| 17 | +}; |
| 18 | + |
| 19 | +/** |
| 20 | + * The Standard Schema types interface. |
| 21 | + */ |
| 22 | +interface StandardSchemaV1Types<Input = unknown, Output = Input> { |
| 23 | + /** |
| 24 | + * The input type of the schema. |
| 25 | + */ |
| 26 | + readonly input: Input; |
| 27 | + /** |
| 28 | + * The output type of the schema. |
| 29 | + */ |
| 30 | + readonly output: Output; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * The Standard Schema properties interface. |
| 35 | + */ |
| 36 | +interface StandardSchemaV1Props<Input = unknown, Output = Input> { |
| 37 | + /** |
| 38 | + * Inferred types associated with the schema. |
| 39 | + */ |
| 40 | + readonly types?: StandardSchemaV1Types<Input, Output> | undefined; |
| 41 | + /** |
| 42 | + * Validates unknown input values. |
| 43 | + */ |
| 44 | + readonly validate: (value: unknown) => StandardSchemaV1Result<Output> | Promise<StandardSchemaV1Result<Output>>; |
| 45 | + /** |
| 46 | + * The vendor name of the schema library. |
| 47 | + */ |
| 48 | + readonly vendor: string; |
| 49 | + /** |
| 50 | + * The version number of the standard. |
| 51 | + */ |
| 52 | + readonly version: 1; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * The result interface of the validate function. |
| 57 | + */ |
| 58 | +type StandardSchemaV1Result<Output> = StandardSchemaV1SuccessResult<Output> | StandardSchemaV1FailureResult; |
| 59 | +/** |
| 60 | + * The result interface if validation succeeds. |
| 61 | + */ |
| 62 | +interface StandardSchemaV1SuccessResult<Output> { |
| 63 | + /** |
| 64 | + * The non-existent issues. |
| 65 | + */ |
| 66 | + readonly issues?: undefined; |
| 67 | + /** |
| 68 | + * The typed output value. |
| 69 | + */ |
| 70 | + readonly value: Output; |
| 71 | +} |
| 72 | +/** |
| 73 | + * The result interface if validation fails. |
| 74 | + */ |
| 75 | +interface StandardSchemaV1FailureResult { |
| 76 | + /** |
| 77 | + * The issues of failed validation. |
| 78 | + */ |
| 79 | + readonly issues: ReadonlyArray<StandardSchemaV1Issue>; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * The issue interface of the failure output. |
| 84 | + */ |
| 85 | +export interface StandardSchemaV1Issue { |
| 86 | + /** |
| 87 | + * The error message of the issue. |
| 88 | + */ |
| 89 | + readonly message: string; |
| 90 | + /** |
| 91 | + * The path of the issue, if any. |
| 92 | + */ |
| 93 | + readonly path?: ReadonlyArray<PropertyKey | StandardSchemaV1PathSegment> | undefined; |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Internal normalized issue type |
| 98 | + * 路径已经被扁平化为 string[] |
| 99 | + */ |
| 100 | +export interface StandardSchemaV1NormalizedIssue { |
| 101 | + /** 错误信息 */ |
| 102 | + readonly message: string; |
| 103 | + /** 扁平化路径 */ |
| 104 | + readonly path: readonly string[]; |
| 105 | +} |
| 106 | +/** |
| 107 | + * The path segment interface of the issue. |
| 108 | + */ |
| 109 | +interface StandardSchemaV1PathSegment { |
| 110 | + /** |
| 111 | + * The key representing a path segment. |
| 112 | + */ |
| 113 | + readonly key: PropertyKey; |
| 114 | +} |
| 115 | + |
| 116 | +function isStandardSchema(obj: any): obj is StandardSchemaV1 { |
| 117 | + return obj && obj['~standard'] && typeof obj['~standard'].validate === 'function'; |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Standard Schema Resolver |
| 122 | + * 支持 sync/async validate,同时处理 validateField 和 validateFields |
| 123 | + */ |
| 124 | +export function createStandardResolver<Values = any>( |
| 125 | + schema: StandardSchemaV1<Values, unknown> |
| 126 | +): Middleware<Values, AllPathsKeys<Values>> { |
| 127 | + if (!isStandardSchema(schema)) { |
| 128 | + throw new Error('Invalid StandardSchema object'); |
| 129 | + } |
| 130 | + |
| 131 | + return ({ dispatch, getState }) => |
| 132 | + next => |
| 133 | + async action => { |
| 134 | + if (action.type !== 'validateField' && action.type !== 'validateFields') { |
| 135 | + return next(action); |
| 136 | + } |
| 137 | + |
| 138 | + const state = getState(); |
| 139 | + const result = await Promise.resolve(schema['~standard'].validate(state)); |
| 140 | + |
| 141 | + console.log('result', result); |
| 142 | + |
| 143 | + if (!('issues' in result)) { |
| 144 | + // ✅ 没有错误,清空所有 |
| 145 | + dispatch({ entries: [], type: 'setExternalErrors' }); |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + // 把 issues 转成统一格式 |
| 150 | + const issues: StandardSchemaV1NormalizedIssue[] = |
| 151 | + result.issues?.map(issue => ({ |
| 152 | + message: issue.message, |
| 153 | + path: issue.path?.map(seg => (typeof seg === 'object' && 'key' in seg ? String(seg.key) : String(seg))) || [] |
| 154 | + })) || []; |
| 155 | + |
| 156 | + // === validateField === |
| 157 | + if (action.type === 'validateField') { |
| 158 | + const name = keyOfName(action.name); |
| 159 | + |
| 160 | + const filtered = issues.filter(it => it.path?.join('.') === name || (it.path?.length === 0 && name === 'root')); |
| 161 | + |
| 162 | + dispatchIssues<Values>(dispatch, filtered); |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + // === validateFields === |
| 167 | + if (action.type === 'validateFields') { |
| 168 | + dispatchIssues<Values>(dispatch, issues); |
| 169 | + } |
| 170 | + }; |
| 171 | +} |
0 commit comments