Skip to content

Commit

Permalink
update docs and improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
swain committed Aug 22, 2022
1 parent a50653d commit 4605042
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@ const server = new Koa()
.listen();
```

By default, `implementSchema` will perform input validation on all of your routes, using the defined `Request` schemas.
To customize this input validation, specify a `parse` function:

```typescript
implementSchema(Schema, {
// ...
parse: (ctx, { endpoint, schema, data }) => {
// Validate `data` against the `schema`.
// If the data is valid, return it, otherwise throw.
},
});
```

### Axios Client Generation

Projects that want to safely consume a service that uses `one-schema` can perform introspection using `fetch-remote-schema`.
Expand Down
45 changes: 42 additions & 3 deletions src/integration.koa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ test('introspection', async () => {
);
});

test('default parsing for POST', async () => {
test('default parsing for POST fails invalid data', async () => {
await executeTest(
{
parse: undefined,
Expand All @@ -266,7 +266,7 @@ test('default parsing for POST', async () => {
},
},
async (client) => {
const result = await client.post('/posts');
const result = await client.post('/posts', {});

expect(result).toMatchObject({
status: 400,
Expand All @@ -276,7 +276,28 @@ test('default parsing for POST', async () => {
);
});

test('default parsing for GET', async () => {
test('default parsing for POST allows valid data', async () => {
await executeTest(
{
parse: undefined,
implementation: {
'POST /posts': (ctx) => ctx.request.body,
},
},
async (client) => {
const result = await client.post('/posts', {
id: 'some-id',
message: 'some-message',
});

expect(result).toMatchObject({
status: 200,
});
},
);
});

test('default parsing for GET fails invalid data', async () => {
await executeTest(
{
parse: undefined,
Expand All @@ -296,3 +317,21 @@ test('default parsing for GET', async () => {
},
);
});

test('default parsing for GET allows valid data', async () => {
await executeTest(
{
parse: undefined,
implementation: {
'GET /posts': (ctx) => ctx.request.body,
},
},
async (client) => {
const result = await client.get('/posts?input=something');

expect(result).toMatchObject({
status: 200,
});
},
);
});

0 comments on commit 4605042

Please sign in to comment.