@struktur/sdk 2.1.2 → 2.3.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/dist/artifacts/fileToArtifact.d.ts +8 -0
- package/dist/artifacts/fileToArtifact.d.ts.map +1 -0
- package/dist/artifacts/input.d.ts +60 -0
- package/dist/artifacts/input.d.ts.map +1 -0
- package/{src/artifacts/providers.ts → dist/artifacts/providers.d.ts} +2 -4
- package/dist/artifacts/providers.d.ts.map +1 -0
- package/dist/artifacts/urlToArtifact.d.ts +3 -0
- package/dist/artifacts/urlToArtifact.d.ts.map +1 -0
- package/dist/auth/config.d.ts +34 -0
- package/dist/auth/config.d.ts.map +1 -0
- package/dist/auth/tokens.d.ts +18 -0
- package/dist/auth/tokens.d.ts.map +1 -0
- package/dist/chunking/ArtifactBatcher.d.ts +11 -0
- package/dist/chunking/ArtifactBatcher.d.ts.map +1 -0
- package/dist/chunking/ArtifactSplitter.d.ts +10 -0
- package/dist/chunking/ArtifactSplitter.d.ts.map +1 -0
- package/dist/debug/logger.d.ts +169 -0
- package/dist/debug/logger.d.ts.map +1 -0
- package/dist/extract.d.ts +3 -0
- package/dist/extract.d.ts.map +1 -0
- package/dist/fields.d.ts +75 -0
- package/dist/fields.d.ts.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5603 -0
- package/dist/index.js.map +1 -0
- package/dist/llm/LLMClient.d.ts +40 -0
- package/dist/llm/LLMClient.d.ts.map +1 -0
- package/dist/llm/RetryingRunner.d.ts +37 -0
- package/dist/llm/RetryingRunner.d.ts.map +1 -0
- package/dist/llm/message.d.ts +12 -0
- package/dist/llm/message.d.ts.map +1 -0
- package/dist/llm/models.d.ts +13 -0
- package/dist/llm/models.d.ts.map +1 -0
- package/dist/llm/resolveModel.d.ts +3 -0
- package/dist/llm/resolveModel.d.ts.map +1 -0
- package/dist/merge/Deduplicator.d.ts +4 -0
- package/dist/merge/Deduplicator.d.ts.map +1 -0
- package/dist/merge/SmartDataMerger.d.ts +7 -0
- package/dist/merge/SmartDataMerger.d.ts.map +1 -0
- package/dist/parsers/collect.d.ts +7 -0
- package/dist/parsers/collect.d.ts.map +1 -0
- package/{src/parsers/index.ts → dist/parsers/index.d.ts} +1 -0
- package/dist/parsers/index.d.ts.map +1 -0
- package/dist/parsers/mime.d.ts +12 -0
- package/dist/parsers/mime.d.ts.map +1 -0
- package/dist/parsers/npm.d.ts +16 -0
- package/dist/parsers/npm.d.ts.map +1 -0
- package/dist/parsers/pdf.d.ts +36 -0
- package/dist/parsers/pdf.d.ts.map +1 -0
- package/dist/parsers/runner.d.ts +4 -0
- package/dist/parsers/runner.d.ts.map +1 -0
- package/dist/parsers/types.d.ts +27 -0
- package/dist/parsers/types.d.ts.map +1 -0
- package/dist/parsers.d.ts +1 -0
- package/dist/parsers.js +492 -0
- package/dist/parsers.js.map +1 -0
- package/dist/prompts/DeduplicationPrompt.d.ts +5 -0
- package/dist/prompts/DeduplicationPrompt.d.ts.map +1 -0
- package/dist/prompts/ExtractorPrompt.d.ts +6 -0
- package/dist/prompts/ExtractorPrompt.d.ts.map +1 -0
- package/dist/prompts/ParallelMergerPrompt.d.ts +5 -0
- package/dist/prompts/ParallelMergerPrompt.d.ts.map +1 -0
- package/dist/prompts/SequentialExtractorPrompt.d.ts +6 -0
- package/dist/prompts/SequentialExtractorPrompt.d.ts.map +1 -0
- package/dist/prompts/formatArtifacts.d.ts +3 -0
- package/dist/prompts/formatArtifacts.d.ts.map +1 -0
- package/dist/strategies/DoublePassAutoMergeStrategy.d.ts +23 -0
- package/dist/strategies/DoublePassAutoMergeStrategy.d.ts.map +1 -0
- package/dist/strategies/DoublePassStrategy.d.ts +22 -0
- package/dist/strategies/DoublePassStrategy.d.ts.map +1 -0
- package/dist/strategies/ParallelAutoMergeStrategy.d.ts +27 -0
- package/dist/strategies/ParallelAutoMergeStrategy.d.ts.map +1 -0
- package/dist/strategies/ParallelStrategy.d.ts +22 -0
- package/dist/strategies/ParallelStrategy.d.ts.map +1 -0
- package/dist/strategies/SequentialAutoMergeStrategy.d.ts +22 -0
- package/dist/strategies/SequentialAutoMergeStrategy.d.ts.map +1 -0
- package/dist/strategies/SequentialStrategy.d.ts +20 -0
- package/dist/strategies/SequentialStrategy.d.ts.map +1 -0
- package/dist/strategies/SimpleStrategy.d.ts +18 -0
- package/dist/strategies/SimpleStrategy.d.ts.map +1 -0
- package/dist/strategies/agent/AgentStrategy.d.ts +44 -0
- package/dist/strategies/agent/AgentStrategy.d.ts.map +1 -0
- package/dist/strategies/agent/AgentTools.d.ts +55 -0
- package/dist/strategies/agent/AgentTools.d.ts.map +1 -0
- package/dist/strategies/agent/ArtifactFilesystem.d.ts +51 -0
- package/dist/strategies/agent/ArtifactFilesystem.d.ts.map +1 -0
- package/dist/strategies/agent/index.d.ts +4 -0
- package/dist/strategies/agent/index.d.ts.map +1 -0
- package/dist/strategies/concurrency.d.ts +2 -0
- package/dist/strategies/concurrency.d.ts.map +1 -0
- package/{src/strategies/index.ts → dist/strategies/index.d.ts} +2 -0
- package/dist/strategies/index.d.ts.map +1 -0
- package/dist/strategies/utils.d.ts +39 -0
- package/dist/strategies/utils.d.ts.map +1 -0
- package/dist/strategies.d.ts +1 -0
- package/dist/strategies.js +3930 -0
- package/dist/strategies.js.map +1 -0
- package/dist/tokenization.d.ts +11 -0
- package/dist/tokenization.d.ts.map +1 -0
- package/dist/types.d.ts +178 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/validation/validator.d.ts +20 -0
- package/dist/validation/validator.d.ts.map +1 -0
- package/package.json +30 -14
- package/src/agent-cli-integration.test.ts +0 -47
- package/src/agent-export.test.ts +0 -17
- package/src/agent-tool-labels.test.ts +0 -50
- package/src/artifacts/AGENTS.md +0 -16
- package/src/artifacts/fileToArtifact.test.ts +0 -37
- package/src/artifacts/fileToArtifact.ts +0 -44
- package/src/artifacts/input.test.ts +0 -243
- package/src/artifacts/input.ts +0 -360
- package/src/artifacts/providers.test.ts +0 -19
- package/src/artifacts/urlToArtifact.test.ts +0 -23
- package/src/artifacts/urlToArtifact.ts +0 -19
- package/src/auth/AGENTS.md +0 -11
- package/src/auth/config.test.ts +0 -132
- package/src/auth/config.ts +0 -186
- package/src/auth/tokens.test.ts +0 -58
- package/src/auth/tokens.ts +0 -229
- package/src/chunking/AGENTS.md +0 -11
- package/src/chunking/ArtifactBatcher.test.ts +0 -22
- package/src/chunking/ArtifactBatcher.ts +0 -110
- package/src/chunking/ArtifactSplitter.test.ts +0 -38
- package/src/chunking/ArtifactSplitter.ts +0 -151
- package/src/debug/AGENTS.md +0 -79
- package/src/debug/logger.test.ts +0 -244
- package/src/debug/logger.ts +0 -211
- package/src/extract.test.ts +0 -22
- package/src/extract.ts +0 -150
- package/src/fields.test.ts +0 -681
- package/src/fields.ts +0 -246
- package/src/index.test.ts +0 -20
- package/src/index.ts +0 -110
- package/src/llm/AGENTS.md +0 -9
- package/src/llm/LLMClient.test.ts +0 -394
- package/src/llm/LLMClient.ts +0 -264
- package/src/llm/RetryingRunner.test.ts +0 -174
- package/src/llm/RetryingRunner.ts +0 -270
- package/src/llm/message.test.ts +0 -42
- package/src/llm/message.ts +0 -47
- package/src/llm/models.test.ts +0 -82
- package/src/llm/models.ts +0 -190
- package/src/llm/resolveModel.ts +0 -86
- package/src/merge/AGENTS.md +0 -6
- package/src/merge/Deduplicator.test.ts +0 -108
- package/src/merge/Deduplicator.ts +0 -45
- package/src/merge/SmartDataMerger.test.ts +0 -177
- package/src/merge/SmartDataMerger.ts +0 -56
- package/src/parsers/AGENTS.md +0 -58
- package/src/parsers/collect.test.ts +0 -56
- package/src/parsers/collect.ts +0 -31
- package/src/parsers/mime.test.ts +0 -91
- package/src/parsers/mime.ts +0 -137
- package/src/parsers/npm.ts +0 -26
- package/src/parsers/pdf.test.ts +0 -394
- package/src/parsers/pdf.ts +0 -194
- package/src/parsers/runner.test.ts +0 -95
- package/src/parsers/runner.ts +0 -177
- package/src/parsers/types.ts +0 -29
- package/src/prompts/AGENTS.md +0 -8
- package/src/prompts/DeduplicationPrompt.test.ts +0 -41
- package/src/prompts/DeduplicationPrompt.ts +0 -37
- package/src/prompts/ExtractorPrompt.test.ts +0 -21
- package/src/prompts/ExtractorPrompt.ts +0 -72
- package/src/prompts/ParallelMergerPrompt.test.ts +0 -8
- package/src/prompts/ParallelMergerPrompt.ts +0 -37
- package/src/prompts/SequentialExtractorPrompt.test.ts +0 -24
- package/src/prompts/SequentialExtractorPrompt.ts +0 -82
- package/src/prompts/formatArtifacts.test.ts +0 -39
- package/src/prompts/formatArtifacts.ts +0 -46
- package/src/strategies/AGENTS.md +0 -6
- package/src/strategies/DoublePassAutoMergeStrategy.test.ts +0 -53
- package/src/strategies/DoublePassAutoMergeStrategy.ts +0 -410
- package/src/strategies/DoublePassStrategy.test.ts +0 -48
- package/src/strategies/DoublePassStrategy.ts +0 -266
- package/src/strategies/ParallelAutoMergeStrategy.test.ts +0 -152
- package/src/strategies/ParallelAutoMergeStrategy.ts +0 -345
- package/src/strategies/ParallelStrategy.test.ts +0 -61
- package/src/strategies/ParallelStrategy.ts +0 -208
- package/src/strategies/SequentialAutoMergeStrategy.test.ts +0 -66
- package/src/strategies/SequentialAutoMergeStrategy.ts +0 -325
- package/src/strategies/SequentialStrategy.test.ts +0 -53
- package/src/strategies/SequentialStrategy.ts +0 -142
- package/src/strategies/SimpleStrategy.test.ts +0 -46
- package/src/strategies/SimpleStrategy.ts +0 -94
- package/src/strategies/concurrency.test.ts +0 -16
- package/src/strategies/concurrency.ts +0 -14
- package/src/strategies/index.test.ts +0 -20
- package/src/strategies/utils.test.ts +0 -76
- package/src/strategies/utils.ts +0 -95
- package/src/tokenization.test.ts +0 -119
- package/src/tokenization.ts +0 -71
- package/src/types.test.ts +0 -25
- package/src/types.ts +0 -174
- package/src/validation/AGENTS.md +0 -7
- package/src/validation/validator.test.ts +0 -204
- package/src/validation/validator.ts +0 -90
- package/tsconfig.json +0 -22
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
import { test, expect } from "bun:test";
|
|
2
|
-
import type { JSONSchemaType, ErrorObject } from "ajv";
|
|
3
|
-
import { createAjv, validateOrThrow, SchemaValidationError, isRequiredError, validateAllowingMissingRequired } from "./validator";
|
|
4
|
-
|
|
5
|
-
type Person = {
|
|
6
|
-
name: string;
|
|
7
|
-
age: number;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
const personSchema: JSONSchemaType<Person> = {
|
|
11
|
-
type: "object",
|
|
12
|
-
properties: {
|
|
13
|
-
name: { type: "string" },
|
|
14
|
-
age: { type: "number" },
|
|
15
|
-
},
|
|
16
|
-
required: ["name", "age"],
|
|
17
|
-
additionalProperties: false,
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
type NestedSchema = {
|
|
21
|
-
user: {
|
|
22
|
-
name: string;
|
|
23
|
-
email: string;
|
|
24
|
-
};
|
|
25
|
-
items: string[];
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const nestedSchema: JSONSchemaType<NestedSchema> = {
|
|
29
|
-
type: "object",
|
|
30
|
-
properties: {
|
|
31
|
-
user: {
|
|
32
|
-
type: "object",
|
|
33
|
-
properties: {
|
|
34
|
-
name: { type: "string" },
|
|
35
|
-
email: { type: "string", format: "email" },
|
|
36
|
-
},
|
|
37
|
-
required: ["name", "email"],
|
|
38
|
-
},
|
|
39
|
-
items: {
|
|
40
|
-
type: "array",
|
|
41
|
-
items: { type: "string" },
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
required: ["user", "items"],
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
test("validateOrThrow returns typed data for valid input", () => {
|
|
48
|
-
const ajv = createAjv();
|
|
49
|
-
const data = validateOrThrow<Person>(ajv, personSchema, {
|
|
50
|
-
name: "Ada",
|
|
51
|
-
age: 33,
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
expect(data.name).toBe("Ada");
|
|
55
|
-
expect(data.age).toBe(33);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test("validateOrThrow throws SchemaValidationError for invalid input", () => {
|
|
59
|
-
const ajv = createAjv();
|
|
60
|
-
try {
|
|
61
|
-
validateOrThrow<Person>(ajv, personSchema, { name: "Ada" });
|
|
62
|
-
throw new Error("Expected validation error");
|
|
63
|
-
} catch (error) {
|
|
64
|
-
expect(error).toBeInstanceOf(SchemaValidationError);
|
|
65
|
-
const validationError = error as SchemaValidationError;
|
|
66
|
-
expect(validationError.errors.length).toBeGreaterThan(0);
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
test("createAjv supports common formats", () => {
|
|
71
|
-
const ajv = createAjv();
|
|
72
|
-
const schema: JSONSchemaType<string> = { type: "string", format: "email" };
|
|
73
|
-
|
|
74
|
-
const data = validateOrThrow<string>(ajv, schema, "test@example.com");
|
|
75
|
-
expect(data).toBe("test@example.com");
|
|
76
|
-
|
|
77
|
-
try {
|
|
78
|
-
validateOrThrow<string>(ajv, schema, "not-an-email");
|
|
79
|
-
throw new Error("Expected validation error");
|
|
80
|
-
} catch (error) {
|
|
81
|
-
expect(error).toBeInstanceOf(SchemaValidationError);
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
test("createAjv supports artifact-id format", () => {
|
|
86
|
-
const ajv = createAjv();
|
|
87
|
-
const schema: JSONSchemaType<string> = { type: "string", format: "artifact-id" };
|
|
88
|
-
|
|
89
|
-
const validData = validateOrThrow<string>(ajv, schema, "artifact:123456/images/image1.jpg");
|
|
90
|
-
expect(validData).toBe("artifact:123456/images/image1.jpg");
|
|
91
|
-
|
|
92
|
-
const validData2 = validateOrThrow<string>(ajv, schema, "artifact:abc-xyz/images/image10.png");
|
|
93
|
-
expect(validData2).toBe("artifact:abc-xyz/images/image10.png");
|
|
94
|
-
|
|
95
|
-
try {
|
|
96
|
-
validateOrThrow<string>(ajv, schema, "not-an-artifact-id");
|
|
97
|
-
throw new Error("Expected validation error");
|
|
98
|
-
} catch (error) {
|
|
99
|
-
expect(error).toBeInstanceOf(SchemaValidationError);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
try {
|
|
103
|
-
validateOrThrow<string>(ajv, schema, "artifact:123/images/image");
|
|
104
|
-
throw new Error("Expected validation error");
|
|
105
|
-
} catch (error) {
|
|
106
|
-
expect(error).toBeInstanceOf(SchemaValidationError);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
try {
|
|
110
|
-
validateOrThrow<string>(ajv, schema, "https://example.com/image.jpg");
|
|
111
|
-
throw new Error("Expected validation error");
|
|
112
|
-
} catch (error) {
|
|
113
|
-
expect(error).toBeInstanceOf(SchemaValidationError);
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
test("isRequiredError identifies required constraint violations", () => {
|
|
118
|
-
const requiredError = {
|
|
119
|
-
keyword: "required",
|
|
120
|
-
params: { missingProperty: "name" },
|
|
121
|
-
instancePath: "",
|
|
122
|
-
schemaPath: "#/required"
|
|
123
|
-
} as unknown as ErrorObject;
|
|
124
|
-
const typeError = {
|
|
125
|
-
keyword: "type",
|
|
126
|
-
params: { type: "string" },
|
|
127
|
-
instancePath: "/age",
|
|
128
|
-
schemaPath: "#/properties/age/type"
|
|
129
|
-
} as unknown as ErrorObject;
|
|
130
|
-
|
|
131
|
-
expect(isRequiredError(requiredError)).toBe(true);
|
|
132
|
-
expect(isRequiredError(typeError)).toBe(false);
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
test("validateAllowingMissingRequired ignores required errors but catches type errors", () => {
|
|
136
|
-
const ajv = createAjv();
|
|
137
|
-
|
|
138
|
-
const resultMissingRequired = validateAllowingMissingRequired<Person>(
|
|
139
|
-
ajv,
|
|
140
|
-
personSchema,
|
|
141
|
-
{ name: "Ada" }
|
|
142
|
-
);
|
|
143
|
-
expect(resultMissingRequired.valid).toBe(true);
|
|
144
|
-
if (resultMissingRequired.valid) {
|
|
145
|
-
expect(resultMissingRequired.data.name).toBe("Ada");
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const resultTypeError = validateAllowingMissingRequired<Person>(
|
|
149
|
-
ajv,
|
|
150
|
-
personSchema,
|
|
151
|
-
{ name: "Ada", age: "thirty-three" }
|
|
152
|
-
);
|
|
153
|
-
expect(resultTypeError.valid).toBe(false);
|
|
154
|
-
if (!resultTypeError.valid) {
|
|
155
|
-
expect(resultTypeError.errors.some(e => e.keyword === "type")).toBe(true);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const resultValid = validateAllowingMissingRequired<Person>(
|
|
159
|
-
ajv,
|
|
160
|
-
personSchema,
|
|
161
|
-
{ name: "Ada", age: 33 }
|
|
162
|
-
);
|
|
163
|
-
expect(resultValid.valid).toBe(true);
|
|
164
|
-
if (resultValid.valid) {
|
|
165
|
-
expect(resultValid.data.name).toBe("Ada");
|
|
166
|
-
expect(resultValid.data.age).toBe(33);
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
test("validateAllowingMissingRequired handles format errors", () => {
|
|
171
|
-
const ajv = createAjv();
|
|
172
|
-
|
|
173
|
-
const result = validateAllowingMissingRequired<NestedSchema>(
|
|
174
|
-
ajv,
|
|
175
|
-
nestedSchema,
|
|
176
|
-
{
|
|
177
|
-
user: { name: "Ada", email: "not-an-email" },
|
|
178
|
-
items: ["a", "b"]
|
|
179
|
-
}
|
|
180
|
-
);
|
|
181
|
-
|
|
182
|
-
expect(result.valid).toBe(false);
|
|
183
|
-
if (!result.valid) {
|
|
184
|
-
expect(result.errors.some(e => e.keyword === "format")).toBe(true);
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
test("validateAllowingMissingRequired handles nested required field errors", () => {
|
|
189
|
-
const ajv = createAjv();
|
|
190
|
-
|
|
191
|
-
const result = validateAllowingMissingRequired<NestedSchema>(
|
|
192
|
-
ajv,
|
|
193
|
-
nestedSchema,
|
|
194
|
-
{
|
|
195
|
-
user: { name: "Ada" },
|
|
196
|
-
items: ["a"]
|
|
197
|
-
}
|
|
198
|
-
);
|
|
199
|
-
|
|
200
|
-
expect(result.valid).toBe(true);
|
|
201
|
-
if (result.valid) {
|
|
202
|
-
expect(result.data.user.name).toBe("Ada");
|
|
203
|
-
}
|
|
204
|
-
});
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import Ajv, { type AnySchema, type ErrorObject, type JSONSchemaType } from "ajv";
|
|
2
|
-
import addFormats from "ajv-formats";
|
|
3
|
-
|
|
4
|
-
export type ValidationErrors = ErrorObject[];
|
|
5
|
-
|
|
6
|
-
export type ValidationMode = 'strict' | 'lenient';
|
|
7
|
-
|
|
8
|
-
export type ValidationResult<T> =
|
|
9
|
-
| { valid: true; data: T }
|
|
10
|
-
| { valid: false; errors: ErrorObject[] };
|
|
11
|
-
|
|
12
|
-
export class SchemaValidationError extends Error {
|
|
13
|
-
public readonly errors: ValidationErrors;
|
|
14
|
-
|
|
15
|
-
constructor(message: string, errors: ValidationErrors) {
|
|
16
|
-
super(message);
|
|
17
|
-
this.name = "SchemaValidationError";
|
|
18
|
-
this.errors = errors;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const ARTIFACT_ID_PATTERN = /^artifact:[^/]+\/images\/image\d+\.\w+$/;
|
|
23
|
-
|
|
24
|
-
export const createAjv = () => {
|
|
25
|
-
const ajv = new Ajv({
|
|
26
|
-
allErrors: true,
|
|
27
|
-
strict: false,
|
|
28
|
-
allowUnionTypes: true,
|
|
29
|
-
});
|
|
30
|
-
addFormats(ajv);
|
|
31
|
-
|
|
32
|
-
ajv.addFormat("artifact-id", {
|
|
33
|
-
type: "string",
|
|
34
|
-
validate: (data: string) => ARTIFACT_ID_PATTERN.test(data),
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
return ajv;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export type SchemaInput<T> = JSONSchemaType<T> | AnySchema;
|
|
41
|
-
|
|
42
|
-
export const validateOrThrow = <T>(
|
|
43
|
-
ajv: Ajv,
|
|
44
|
-
schema: SchemaInput<T>,
|
|
45
|
-
data: unknown
|
|
46
|
-
): T => {
|
|
47
|
-
const validate = ajv.compile<T>(schema);
|
|
48
|
-
const valid = validate(data);
|
|
49
|
-
|
|
50
|
-
if (!valid) {
|
|
51
|
-
const errors = validate.errors ?? [];
|
|
52
|
-
const message = "Schema validation failed";
|
|
53
|
-
throw new SchemaValidationError(message, errors);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return data as T;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export const isRequiredError = (error: ErrorObject): boolean => {
|
|
60
|
-
return error.keyword === "required";
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export const validateAllowingMissingRequired = <T>(
|
|
64
|
-
ajv: Ajv,
|
|
65
|
-
schema: SchemaInput<T>,
|
|
66
|
-
data: unknown,
|
|
67
|
-
isFinalAttempt: boolean = true
|
|
68
|
-
): ValidationResult<T> => {
|
|
69
|
-
const validate = ajv.compile<T>(schema);
|
|
70
|
-
const valid = validate(data);
|
|
71
|
-
|
|
72
|
-
if (valid) {
|
|
73
|
-
return { valid: true, data: data as T };
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const errors = validate.errors ?? [];
|
|
77
|
-
const nonRequiredErrors = errors.filter((error) => !isRequiredError(error));
|
|
78
|
-
|
|
79
|
-
if (nonRequiredErrors.length === 0) {
|
|
80
|
-
// Only required field errors
|
|
81
|
-
// On final attempt, accept partial data
|
|
82
|
-
// On non-final attempts, return invalid to trigger retry
|
|
83
|
-
if (isFinalAttempt) {
|
|
84
|
-
return { valid: true, data: data as T };
|
|
85
|
-
}
|
|
86
|
-
return { valid: false, errors };
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return { valid: false, errors: nonRequiredErrors };
|
|
90
|
-
};
|
package/tsconfig.json
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"lib": ["ESNext"],
|
|
4
|
-
"target": "ESNext",
|
|
5
|
-
"module": "Preserve",
|
|
6
|
-
"moduleDetection": "force",
|
|
7
|
-
"jsx": "react-jsx",
|
|
8
|
-
"allowJs": true,
|
|
9
|
-
"moduleResolution": "bundler",
|
|
10
|
-
"allowImportingTsExtensions": true,
|
|
11
|
-
"verbatimModuleSyntax": true,
|
|
12
|
-
"noEmit": true,
|
|
13
|
-
"strict": true,
|
|
14
|
-
"skipLibCheck": true,
|
|
15
|
-
"noFallthroughCasesInSwitch": true,
|
|
16
|
-
"noUncheckedIndexedAccess": true,
|
|
17
|
-
"noImplicitOverride": true,
|
|
18
|
-
"noUnusedLocals": false,
|
|
19
|
-
"noUnusedParameters": false,
|
|
20
|
-
"noPropertyAccessFromIndexSignature": false
|
|
21
|
-
}
|
|
22
|
-
}
|