|
| 1 | +import { StandardSchemaV1 } from "@standard-schema/spec"; |
| 2 | + |
| 3 | +import { IValidation } from "../IValidation"; |
| 4 | + |
| 5 | +export const _createStandardSchema = <T>( |
| 6 | + fn: (input: unknown) => IValidation<T>, |
| 7 | +) => |
| 8 | + Object.assign(fn, { |
| 9 | + "~standard": { |
| 10 | + version: 1, |
| 11 | + vendor: "typia", |
| 12 | + validate: (input: unknown): StandardSchemaV1.Result<T> => { |
| 13 | + const result = fn(input); |
| 14 | + if (result.success) { |
| 15 | + return { |
| 16 | + value: result.data, |
| 17 | + } satisfies StandardSchemaV1.SuccessResult<T>; |
| 18 | + } else { |
| 19 | + return { |
| 20 | + issues: result.errors.map((error) => ({ |
| 21 | + message: `expected ${error.expected}, got ${error.value}`, |
| 22 | + path: typiaPathToStandardSchemaPath(error.path), |
| 23 | + })), |
| 24 | + } satisfies StandardSchemaV1.FailureResult; |
| 25 | + } |
| 26 | + }, |
| 27 | + }, |
| 28 | + } satisfies StandardSchemaV1<unknown, T>); |
| 29 | + |
| 30 | +enum PathParserState { |
| 31 | + // Start of a new segment |
| 32 | + // When the parser is in this state, |
| 33 | + // the pointer must point `.` or `[` or equal to length of the path |
| 34 | + Start, |
| 35 | + // Parsing a property key (`.hoge`) |
| 36 | + Property, |
| 37 | + // Parsing a string key (`["fuga"]`) |
| 38 | + StringKey, |
| 39 | + // Parsing a number key (`[42]`) |
| 40 | + NumberKey, |
| 41 | +} |
| 42 | + |
| 43 | +const typiaPathToStandardSchemaPath = ( |
| 44 | + path: string, |
| 45 | +): ReadonlyArray<StandardSchemaV1.PathSegment> => { |
| 46 | + if (!path.startsWith("$input")) { |
| 47 | + throw new Error(`Invalid path: ${JSON.stringify(path)}`); |
| 48 | + } |
| 49 | + |
| 50 | + const segments: StandardSchemaV1.PathSegment[] = []; |
| 51 | + let currentSegment = ""; |
| 52 | + let state: PathParserState = PathParserState.Start; |
| 53 | + let index = "$input".length - 1; |
| 54 | + while (index < path.length - 1) { |
| 55 | + index++; |
| 56 | + const char = path[index]; |
| 57 | + |
| 58 | + if (state === PathParserState.Property) { |
| 59 | + if (char === "." || char === "[") { |
| 60 | + // End of property |
| 61 | + segments.push({ |
| 62 | + key: currentSegment, |
| 63 | + }); |
| 64 | + state = PathParserState.Start; |
| 65 | + } else if (index === path.length - 1) { |
| 66 | + // End of path |
| 67 | + currentSegment += char; |
| 68 | + segments.push({ |
| 69 | + key: currentSegment, |
| 70 | + }); |
| 71 | + index++; |
| 72 | + state = PathParserState.Start; |
| 73 | + } else { |
| 74 | + currentSegment += char; |
| 75 | + } |
| 76 | + } else if (state === PathParserState.StringKey) { |
| 77 | + if (char === '"') { |
| 78 | + // End of string key |
| 79 | + segments.push({ |
| 80 | + key: JSON.parse(currentSegment + char), |
| 81 | + }); |
| 82 | + // Skip `"` and `]` |
| 83 | + index += 2; |
| 84 | + state = PathParserState.Start; |
| 85 | + } else if (char === "\\") { |
| 86 | + // Skip the next character from parsing |
| 87 | + currentSegment += path[index]; |
| 88 | + index++; |
| 89 | + currentSegment += path[index]; |
| 90 | + } else { |
| 91 | + currentSegment += char; |
| 92 | + } |
| 93 | + } else if (state === PathParserState.NumberKey) { |
| 94 | + if (char === "]") { |
| 95 | + // End of number key |
| 96 | + segments.push({ |
| 97 | + key: Number.parseInt(currentSegment), |
| 98 | + }); |
| 99 | + index++; |
| 100 | + state = PathParserState.Start; |
| 101 | + } else { |
| 102 | + currentSegment += char; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if (state === PathParserState.Start && index < path.length - 1) { |
| 107 | + const newChar = path[index]; |
| 108 | + currentSegment = ""; |
| 109 | + if (newChar === "[") { |
| 110 | + if (path[index + 1] === '"') { |
| 111 | + // Start of string key |
| 112 | + // NOTE: Typia uses JSON.stringify for this kind of keys, so `'` will not used as a string delimiter |
| 113 | + state = PathParserState.StringKey; |
| 114 | + index++; |
| 115 | + currentSegment = '"'; |
| 116 | + } else { |
| 117 | + // Start of number key |
| 118 | + state = PathParserState.NumberKey; |
| 119 | + } |
| 120 | + } else if (newChar === ".") { |
| 121 | + // Start of property |
| 122 | + state = PathParserState.Property; |
| 123 | + } else { |
| 124 | + throw new Error("Unreachable: pointer points invalid character"); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (state !== PathParserState.Start) { |
| 130 | + throw new Error(`Failed to parse path: ${JSON.stringify(path)}`); |
| 131 | + } |
| 132 | + |
| 133 | + return segments; |
| 134 | +}; |
0 commit comments