-
Notifications
You must be signed in to change notification settings - Fork 531
Expand file tree
/
Copy pathsafeParseSchema.ts
More file actions
27 lines (23 loc) · 880 Bytes
/
safeParseSchema.ts
File metadata and controls
27 lines (23 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2026 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { schema } from '@cloudbeaver/core-utils';
import type { IFormValidationContext } from './formValidationContext.js';
export function safeParseSchema<T>(zodSchema: schema.ZodType<T>, value: unknown, validation: IFormValidationContext): schema.ZodSafeParseResult<T> {
const result = zodSchema.safeParse(value);
if (!result.success) {
const seenMessages = new Set<string>();
for (const issue of result.error.issues) {
const message = issue.message || schema.prettifyError(result.error);
if (!seenMessages.has(message)) {
seenMessages.add(message);
validation.error(message);
}
}
}
return result;
}