Skip to content

Commit

Permalink
Split JsonAPICondition into its schema and its condition.
Browse files Browse the repository at this point in the history
Include jsonApiConditionSchema in anyConditionSchema.
  • Loading branch information
derekpierre committed Oct 10, 2024
1 parent e908e24 commit 92e5d73
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 36 deletions.
44 changes: 13 additions & 31 deletions packages/taco/src/conditions/base/json-api.ts
Original file line number Diff line number Diff line change
@@ -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<typeof JsonApiConditionSchema>;
export {
JsonApiConditionProps,
jsonApiConditionSchema,
JsonApiConditionType,
jsonPathSchema,
} from '../schemas/json-api';

export class JsonApiCondition extends Condition {
constructor(value: OmitConditionType<JsonApiConditionProps>) {
super(JsonApiConditionSchema, {
super(jsonApiConditionSchema, {
conditionType: JsonApiConditionType,
...value,
});
Expand Down
31 changes: 31 additions & 0 deletions packages/taco/src/conditions/schemas/json-api.ts
Original file line number Diff line number Diff line change
@@ -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<typeof jsonApiConditionSchema>;
2 changes: 2 additions & 0 deletions packages/taco/src/conditions/schemas/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -13,6 +14,7 @@ export const anyConditionSchema: z.ZodSchema = z.lazy(() =>
timeConditionSchema,
contractConditionSchema,
compoundConditionSchema,
jsonApiConditionSchema,
sequentialConditionSchema,
]),
);
10 changes: 5 additions & 5 deletions packages/taco/test/conditions/base/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { describe, expect, it } from 'vitest';

import {
JsonApiCondition,
JsonApiConditionSchema,
jsonApiConditionSchema,
} from '../../../src/conditions/base/json-api';
import { testJsonApiConditionObj } from '../../test-utils';

describe('JsonApiCondition', () => {
describe('validation', () => {
it('accepts a valid schema', () => {
const result = JsonApiCondition.validate(
JsonApiConditionSchema,
jsonApiConditionSchema,
testJsonApiConditionObj,
);

Expand All @@ -26,7 +26,7 @@ describe('JsonApiCondition', () => {
};

const result = JsonApiCondition.validate(
JsonApiConditionSchema,
jsonApiConditionSchema,
badJsonApiObj,
);

Expand All @@ -43,7 +43,7 @@ describe('JsonApiCondition', () => {
it('accepts conditions without query path', () => {
const { query, ...noQueryObj } = testJsonApiConditionObj;
const result = JsonApiCondition.validate(
JsonApiConditionSchema,
jsonApiConditionSchema,
noQueryObj,
);

Expand All @@ -54,7 +54,7 @@ describe('JsonApiCondition', () => {
it('accepts conditions without parameters', () => {
const { query, ...noParamsObj } = testJsonApiConditionObj;
const result = JsonApiCondition.validate(
JsonApiConditionSchema,
jsonApiConditionSchema,
noParamsObj,
);

Expand Down

0 comments on commit 92e5d73

Please sign in to comment.