diff --git a/packages/taco/src/conditions/base/json-api.ts b/packages/taco/src/conditions/base/json-api.ts index 22032cb3..0203284c 100644 --- a/packages/taco/src/conditions/base/json-api.ts +++ b/packages/taco/src/conditions/base/json-api.ts @@ -1,39 +1,21 @@ -import { JSONPath } from '@astronautlabs/jsonpath'; -import { z } from 'zod'; - import { Condition } from '../condition'; -import { OmitConditionType, returnValueTestSchema } from '../shared'; - -export const JsonApiConditionType = 'json-api'; - -const validateJSONPath = (jsonPath: string): boolean => { - try { - JSONPath.parse(jsonPath); - return true; - } catch (error) { - return false; - } -}; - -export const jsonPathSchema = z - .string() - .refine((val) => validateJSONPath(val), { - message: 'Invalid JSONPath expression', - }); - -export const JsonApiConditionSchema = z.object({ - conditionType: z.literal(JsonApiConditionType).default(JsonApiConditionType), - endpoint: z.string().url(), - parameters: z.record(z.string(), z.unknown()).optional(), - query: jsonPathSchema.optional(), - returnValueTest: returnValueTestSchema, // Update to allow multiple return values after expanding supported methods -}); +import { + JsonApiConditionProps, + jsonApiConditionSchema, + JsonApiConditionType, +} from '../schemas/json-api'; +import { OmitConditionType } from '../shared'; -export type JsonApiConditionProps = z.infer; +export { + JsonApiConditionProps, + jsonApiConditionSchema, + JsonApiConditionType, + jsonPathSchema, +} from '../schemas/json-api'; export class JsonApiCondition extends Condition { constructor(value: OmitConditionType) { - super(JsonApiConditionSchema, { + super(jsonApiConditionSchema, { conditionType: JsonApiConditionType, ...value, }); diff --git a/packages/taco/src/conditions/schemas/json-api.ts b/packages/taco/src/conditions/schemas/json-api.ts new file mode 100644 index 00000000..4a9afb82 --- /dev/null +++ b/packages/taco/src/conditions/schemas/json-api.ts @@ -0,0 +1,31 @@ +import { JSONPath } from '@astronautlabs/jsonpath'; +import { z } from 'zod'; + +import { returnValueTestSchema } from './return-value-test'; + +export const JsonApiConditionType = 'json-api'; + +const validateJSONPath = (jsonPath: string): boolean => { + try { + JSONPath.parse(jsonPath); + return true; + } catch (error) { + return false; + } +}; + +export const jsonPathSchema = z + .string() + .refine((val) => validateJSONPath(val), { + message: 'Invalid JSONPath expression', + }); + +export const jsonApiConditionSchema = z.object({ + conditionType: z.literal(JsonApiConditionType).default(JsonApiConditionType), + endpoint: z.string().url(), + parameters: z.record(z.string(), z.unknown()).optional(), + query: jsonPathSchema.optional(), + returnValueTest: returnValueTestSchema, // Update to allow multiple return values after expanding supported methods +}); + +export type JsonApiConditionProps = z.infer; diff --git a/packages/taco/src/conditions/schemas/utils.ts b/packages/taco/src/conditions/schemas/utils.ts index 6f7a1568..c84cc8d3 100644 --- a/packages/taco/src/conditions/schemas/utils.ts +++ b/packages/taco/src/conditions/schemas/utils.ts @@ -3,6 +3,7 @@ import { z } from 'zod'; import { compoundConditionSchema } from '../compound-condition'; import { contractConditionSchema } from './contract'; +import { jsonApiConditionSchema } from './json-api'; import { rpcConditionSchema } from './rpc'; import { sequentialConditionSchema } from './sequential'; import { timeConditionSchema } from './time'; @@ -13,6 +14,7 @@ export const anyConditionSchema: z.ZodSchema = z.lazy(() => timeConditionSchema, contractConditionSchema, compoundConditionSchema, + jsonApiConditionSchema, sequentialConditionSchema, ]), ); diff --git a/packages/taco/test/conditions/base/json-api.test.ts b/packages/taco/test/conditions/base/json-path.test.ts similarity index 100% rename from packages/taco/test/conditions/base/json-api.test.ts rename to packages/taco/test/conditions/base/json-path.test.ts diff --git a/packages/taco/test/conditions/base/json.test.ts b/packages/taco/test/conditions/base/json.test.ts index 0166485a..2f9291d4 100644 --- a/packages/taco/test/conditions/base/json.test.ts +++ b/packages/taco/test/conditions/base/json.test.ts @@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest'; import { JsonApiCondition, - JsonApiConditionSchema, + jsonApiConditionSchema, } from '../../../src/conditions/base/json-api'; import { testJsonApiConditionObj } from '../../test-utils'; @@ -11,7 +11,7 @@ describe('JsonApiCondition', () => { describe('validation', () => { it('accepts a valid schema', () => { const result = JsonApiCondition.validate( - JsonApiConditionSchema, + jsonApiConditionSchema, testJsonApiConditionObj, ); @@ -26,7 +26,7 @@ describe('JsonApiCondition', () => { }; const result = JsonApiCondition.validate( - JsonApiConditionSchema, + jsonApiConditionSchema, badJsonApiObj, ); @@ -43,7 +43,7 @@ describe('JsonApiCondition', () => { it('accepts conditions without query path', () => { const { query, ...noQueryObj } = testJsonApiConditionObj; const result = JsonApiCondition.validate( - JsonApiConditionSchema, + jsonApiConditionSchema, noQueryObj, ); @@ -54,7 +54,7 @@ describe('JsonApiCondition', () => { it('accepts conditions without parameters', () => { const { query, ...noParamsObj } = testJsonApiConditionObj; const result = JsonApiCondition.validate( - JsonApiConditionSchema, + jsonApiConditionSchema, noParamsObj, );