mirror of
https://github.com/sourcebot-dev/sourcebot.git
synced 2025-12-14 21:35:25 +00:00
* add additional config validation * wip bypass stripe checkout for trial * fix stripe trial checkout bypass * fix indexing in progress ui on home page * add subscription checks, more schema validation, and fix issue with complete page * dont display if no indexed repos
40 lines
No EOL
1.2 KiB
TypeScript
40 lines
No EOL
1.2 KiB
TypeScript
import Ajv, { Schema } from "ajv";
|
|
import { z } from "zod";
|
|
|
|
export const createZodConnectionConfigValidator = <T>(jsonSchema: Schema, additionalConfigValidation?: (config: T) => { message: string, isValid: boolean }) => {
|
|
const ajv = new Ajv({
|
|
validateFormats: false,
|
|
});
|
|
const validate = ajv.compile(jsonSchema);
|
|
|
|
return z
|
|
.string()
|
|
.superRefine((data, ctx) => {
|
|
const addIssue = (message: string) => {
|
|
return ctx.addIssue({
|
|
code: "custom",
|
|
message: `Schema validation error: ${message}`
|
|
});
|
|
}
|
|
|
|
let parsed;
|
|
try {
|
|
parsed = JSON.parse(data);
|
|
} catch {
|
|
addIssue("Invalid JSON");
|
|
return;
|
|
}
|
|
|
|
const valid = validate(parsed);
|
|
if (!valid) {
|
|
addIssue(ajv.errorsText(validate.errors));
|
|
}
|
|
|
|
if (additionalConfigValidation) {
|
|
const result = additionalConfigValidation(parsed as T);
|
|
if (!result.isValid) {
|
|
addIssue(result.message);
|
|
}
|
|
}
|
|
});
|
|
}
|