import { JSONSchema, Schema } from 'effect';
// We don't use Unicode property escapes because they are not supported in OpenAI API :-(
// biome-ignore lint/suspicious/noControlCharactersInRegex: YOLO
const REG_EXP = /[^\s\x00-\x1F\x7F-\x9F]+/;
export const VisibleString = Schema.String.pipe(
Schema.filter((s) => (REG_EXP.test(s) ? undefined : 'Expected string with visible characters')),
Schema.brand('VisibleString'),
Schema.annotations({
identifier: 'VisibleString',
title: 'String with visible characters',
description:
'A string that contains at least one visible character (non-whitespace and non-control character).',
jsonSchema: {
minLength: 1,
pattern: REG_EXP.source,
},
arbitrary: () => (fc) => fc.uuid() as never,
})
);
export const UUID = Schema.UUID.pipe(
Schema.compose(VisibleString),
Schema.brand('UUID'),
Schema.annotations({
identifier: 'UUID',
title: 'UUID',
description: 'A string that represents a UUID.',
jsonSchema: {
format: 'uuid',
},
arbitrary: () => (fc) => fc.uuid() as never,
})
);
const defs = {}
const res = JSONSchema.fromAST(UUID.ast, {
definitions: defs,
definitionPath: "#/components/schemas/",
target: "openApi3.1",
})
console.log(res, defs)
import { JSONSchema, Schema } from 'effect';
// We don't use Unicode property escapes because they are not supported in OpenAI API :-(
// biome-ignore lint/suspicious/noControlCharactersInRegex: YOLO
const REG_EXP = /[^\s\x00-\x1F\x7F-\x9F]+/;
export const VisibleString = Schema.String.pipe(
Schema.filter((s) => (REG_EXP.test(s) ? undefined : 'Expected string with visible characters')),
Schema.brand('VisibleString'),
Schema.annotations({
identifier: 'VisibleString',
title: 'String with visible characters',
description:
'A string that contains at least one visible character (non-whitespace and non-control character).',
jsonSchema: {
minLength: 1,
pattern: REG_EXP.source,
},
arbitrary: () => (fc) => fc.uuid() as never,
})
);
export const UUID = Schema.UUID.pipe(
Schema.compose(VisibleString),
Schema.brand('UUID'),
Schema.annotations({
identifier: 'UUID',
title: 'UUID',
description: 'A string that represents a UUID.',
jsonSchema: {
format: 'uuid',
},
arbitrary: () => (fc) => fc.uuid() as never,
})
);
const defs = {}
const res = JSONSchema.fromAST(UUID.ast, {
definitions: defs,
definitionPath: "#/components/schemas/",
target: "openApi3.1",
})
console.log(res, defs)