-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { z } from 'zod'; | ||
|
||
import { Condition } from '../condition'; | ||
import { | ||
OmitConditionType, | ||
returnValueTestSchema, | ||
} from '../shared'; | ||
|
||
export const JsonApiConditionType = 'json-api'; | ||
|
||
export const JsonApiConditionSchema = z.object({ | ||
conditionType: z.literal(JsonApiConditionType).default(JsonApiConditionType), | ||
endpoint: z.string().url(), | ||
parameters: z.record(z.string(), z.unknown()).optional(), | ||
query: z.string().optional(), | ||
returnValueTest: returnValueTestSchema, // Update to allow multiple return values after expanding supported methods | ||
}); | ||
|
||
export type JsonApiConditionProps = z.infer<typeof JsonApiConditionSchema>; | ||
|
||
export class JsonApiCondition extends Condition { | ||
constructor(value: OmitConditionType<JsonApiConditionProps>) { | ||
super(JsonApiConditionSchema, { | ||
conditionType: JsonApiConditionType, | ||
...value, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { | ||
JsonApiCondition, | ||
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, | ||
testJsonApiConditionObj, | ||
); | ||
|
||
expect(result.error).toBeUndefined(); | ||
expect(result.data).toEqual(testJsonApiConditionObj); | ||
}); | ||
|
||
it('rejects an invalid schema', () => { | ||
const badJsonApiObj = { | ||
...testJsonApiConditionObj, | ||
endpoint: 'not-a-url', | ||
}; | ||
|
||
const result = JsonApiCondition.validate(JsonApiConditionSchema, badJsonApiObj); | ||
|
||
expect(result.error).toBeDefined(); | ||
expect(result.data).toBeUndefined(); | ||
expect(result.error?.format()).toMatchObject({ | ||
endpoint: { | ||
_errors: ['Invalid url'], | ||
}, | ||
}); | ||
}); | ||
|
||
describe('parameters', () => { | ||
it('accepts conditions without query path', () => { | ||
const { query, ...noQueryObj} = testJsonApiConditionObj; | ||
const result = JsonApiCondition.validate( | ||
JsonApiConditionSchema, | ||
noQueryObj | ||
); | ||
|
||
expect(result.error).toBeUndefined(); | ||
expect(result.data).toEqual(noQueryObj); | ||
}); | ||
|
||
it('accepts conditions without parameters', () => { | ||
const { query, ...noParamsObj} = testJsonApiConditionObj; | ||
const result = JsonApiCondition.validate( | ||
JsonApiConditionSchema, | ||
noParamsObj | ||
); | ||
|
||
expect(result.error).toBeUndefined(); | ||
expect(result.data).toEqual(noParamsObj); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters