Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

refactor(json-schema-to-effect-schema): validate through a filter instead of a declaration

jkomynob7592585dde8
diff --git a/ts/packages/json-schema-to-effect-schema/src/index.ts b/ts/packages/json-schema-to-effect-schema/src/index.ts
index 9ae1cb1a4..8f2336e44 100644
--- a/ts/packages/json-schema-to-effect-schema/src/index.ts
+++ b/ts/packages/json-schema-to-effect-schema/src/index.ts
@@ -1,6 +1,6 @@
import { Validator } from '@cfworker/json-schema';
import type { OutputUnit, Schema as InterpreterSchema, SchemaDraft } from '@cfworker/json-schema';
-import { Either, ParseResult, Schema } from 'effect';
+import { Schema } from 'effect';
export type JsonSchemaValidationIssue = {
readonly code: string;
@@ -232,6 +232,14 @@ const propertyNameFromError = (error: OutputUnit): string | undefined =>
const requiredNameFromError = (error: OutputUnit): string | undefined =>
error.error.match(/required property "([^"]+)"/)?.[1];
+const matchesPattern = (pattern: string, key: string): boolean => {
+ try {
+ return new RegExp(pattern, 'u').test(key);
+ } catch {
+ return false;
+ }
+};
+
const propertyIsDeclared = (parent: JsonObject | null, key: string): boolean => {
if (!parent) {
return false;
@@ -243,12 +251,7 @@ const propertyIsDeclared = (parent: JsonObject | null, key: string): boolean =>
return false;
}
- return Object.keys(parent.patternProperties).some(pattern =>
- Either.getOrElse(
- Either.try(() => new RegExp(pattern, 'u').test(key)),
- () => false
- )
- );
+ return Object.keys(parent.patternProperties).some(pattern => matchesPattern(pattern, key));
};
const hasSpecificChildError = (errors: ReadonlyArray<OutputUnit>, error: OutputUnit): boolean =>
@@ -340,35 +343,43 @@ const defaultFormatIssues = (
return `${location}: ${issue.message}`;
});
+// Validation is expressed as a filter over `Schema.Unknown` rather than a
+// `Schema.declare` codec: the interpreter already owns the whole decision, so
+// there is nothing to decode, and a filter reports every failure through the
+// `{ path, message }` shape Effect normalizes for us. `Schema.filter` is also
+// the API that survives the Effect 4 rewrite — where `ParseResult` is split
+// into `SchemaIssue`/`SchemaParser` and declarations lose their encode half —
+// as a rename to `Schema.check(Schema.makeFilter(...))`.
export const jsonSchemaToEffectSchema = (
jsonSchema: JsonObject,
options: JsonSchemaToEffectSchemaOptions = {}
-) => {
+): Schema.Schema<unknown> => {
const normalizedSchema = normalizeJsonSchema(jsonSchema);
const validator = new Validator(normalizedSchema, options.draft ?? '7', false);
const formatIssues = options.formatIssues ?? defaultFormatIssues;
- return Schema.declare([Schema.Unknown], {
- decode: () => (input, _parseOptions, ast) => {
- const validation = Either.try(() => validator.validate(input));
- if (Either.isLeft(validation)) {
- return ParseResult.fail(
- new ParseResult.Type(ast, input, `JSON Schema validation failed: ${validation.left}`)
- );
+ return Schema.Unknown.pipe(
+ Schema.filter(input => {
+ let validation: ReturnType<Validator['validate']>;
+ try {
+ validation = validator.validate(input);
+ } catch (error) {
+ return `JSON Schema validation failed: ${error}`;
}
- if (validation.right.valid) {
- return ParseResult.succeed(input);
+ if (validation.valid) {
+ return true;
}
- const issues = mapValidationErrors(validation.right.errors, normalizedSchema);
+ const issues = mapValidationErrors(validation.errors, normalizedSchema);
const messages = formatIssues(issues);
- const [first, ...rest] = messages.map(message => new ParseResult.Type(ast, input, message));
- return ParseResult.fail(
- first
- ? new ParseResult.Composite(ast, input, [first, ...rest])
- : new ParseResult.Type(ast, input, 'Input does not match the JSON schema.')
- );
- },
- encode: () => input => ParseResult.succeed(input),
- });
+ if (messages.length === 0) {
+ return 'Input does not match the JSON schema.';
+ }
+
+ // `formatIssues` may fan one issue out into several messages, so the
+ // location stays inside the message (as it always has) instead of being
+ // reported as a structural path.
+ return messages.map(message => ({ path: [], message }));
+ })
+ );
};
diff --git a/ts/packages/json-schema-to-effect-schema/test/json-schema-to-effect-schema.test.ts b/ts/packages/json-schema-to-effect-schema/test/json-schema-to-effect-schema.test.ts
index e345ef6e0..118b32203 100644
--- a/ts/packages/json-schema-to-effect-schema/test/json-schema-to-effect-schema.test.ts
+++ b/ts/packages/json-schema-to-effect-schema/test/json-schema-to-effect-schema.test.ts
@@ -1,5 +1,5 @@
import { jsonSchemaToZod } from '@composio/json-schema-to-zod';
-import { Either, Schema } from 'effect';
+import { Option, Schema } from 'effect';
import { describe, expect, it } from 'vitest';
import { jsonSchemaToEffectSchema, type JsonSchemaValidationIssue } from '../src/index';
import { acceptedFor, loadObjectCases } from './fixtures/corpus';
@@ -7,8 +7,8 @@ import { acceptedFor, loadObjectCases } from './fixtures/corpus';
type JsonSchema = Record<string, unknown>;
const effectAccepts = (schema: JsonSchema, input: unknown): boolean =>
- Either.isRight(
- Schema.decodeUnknownEither(jsonSchemaToEffectSchema(schema), { errors: 'all' })(input)
+ Option.isSome(
+ Schema.decodeUnknownOption(jsonSchemaToEffectSchema(schema), { errors: 'all' })(input)
);
const zodAccepts = (schema: JsonSchema, input: unknown): boolean =>
@@ -100,13 +100,13 @@ describe('jsonSchemaToEffectSchema', () => {
},
});
- const result = Schema.decodeUnknownEither(effectSchema, { errors: 'all' })({
+ const result = Schema.decodeUnknownOption(effectSchema, { errors: 'all' })({
email: 42,
profile: { age: 'old', extra: true },
typo: true,
});
- expect(Either.isLeft(result)).toBe(true);
+ expect(Option.isNone(result)).toBe(true);
expect(captured).toEqual(
expect.arrayContaining([
expect.objectContaining({ code: 'type', path: ['email'] }),
@@ -125,16 +125,16 @@ describe('shared cross-SDK object corpus', () => {
const expected = acceptedFor(instance, 'effect');
it(`instance ${index} is ${expected ? 'accepted' : 'rejected'}`, () => {
- const result = Schema.decodeUnknownEither(
+ const result = Schema.decodeUnknownOption(
jsonSchemaToEffectSchema(testCase.schema as JsonSchema),
{ errors: 'all' }
)(instance.input);
- expect(Either.isRight(result)).toBe(expected);
- if (Either.isRight(result) && instance.effect && 'output' in instance.effect) {
+ expect(Option.isSome(result)).toBe(expected);
+ if (Option.isSome(result) && instance.effect && 'output' in instance.effect) {
// Effect decoding is validation-only: a successful decode returns the
// input value unchanged, with no default materialization.
- expect(result.right).toEqual(instance.effect.output);
+ expect(result.value).toEqual(instance.effect.output);
}
});
}