Skip to content
This repository has been archived by the owner on Jun 14, 2021. It is now read-only.

Commit

Permalink
feat: set default values on the request body (closes #127) (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
aimed authored Jul 10, 2019
1 parent 84bdfa5 commit 4aa1607
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
4 changes: 2 additions & 2 deletions example/__tests__/PetsResource.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import request from 'supertest'
import { SlushyFactory } from '../src/SlushyFactory'
import { Slushy } from '@slushy/server'
import request from 'supertest'
import { Context } from '../src/Context'
import { SlushyFactory } from '../src/SlushyFactory'
import { testLoggerFactory } from './testLoggerFactory'

describe('PetsResource', () => {
Expand Down
20 changes: 20 additions & 0 deletions example/__tests__/Validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ describe('Validation', () => {
})
})

describe('body', () => {
describe('default values', () => {
it('should set the default value', async () => {
const response = await request(slushy.app)
.post('/validation/body/set-defaults')
.send({})
expect(response.status).toBe(200)
expect(response.body).toEqual({ default: 'default' })
})

it('should pass values without defaults', async () => {
const response = await request(slushy.app)
.post('/validation/body/set-defaults')
.send({ noDefault: 'noDefault' })
expect(response.status).toBe(200)
expect(response.body).toEqual({ default: 'default', noDefault: 'noDefault' })
})
})
})

describe('path', () => {
it('should accept a request if the parameter is correct', async () => {
const response = await request(slushy.app).get('/validation/path/1/1')
Expand Down
29 changes: 28 additions & 1 deletion example/pet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,34 @@ paths:
properties:
header:
type: string

/validation/body/set-defaults:
post:
operationId: validationBodyDefaultValue
description: Test body default value setter
summary: Test body default value setter
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
default:
type: string
default: default
noDefault:
type: string
responses:
'200':
content:
application/json:
schema:
type: object
properties:
default:
type: string
noDefault:
type: string
/validation/path/{num}/{str}:
get:
operationId: validationPath
Expand Down
13 changes: 13 additions & 0 deletions example/src/ValidationResourceImpl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { SlushyContext } from '@slushy/server'
import { Context } from './Context'
import {
ValidationBodyDefaultValueOK,
ValidationBodyDefaultValueParams,
ValidationBodyDefaultValueResponse,
ValidationHeaderOK,
ValidationHeaderParams,
ValidationHeaderResponse,
Expand All @@ -14,6 +17,16 @@ import {
} from './generated/resources/ValidationResource'

export class ValidationResourceImpl implements ValidationResource<Context> {
public async validationBodyDefaultValue(
params: ValidationBodyDefaultValueParams,
_context: SlushyContext<Context>,
): Promise<ValidationBodyDefaultValueResponse> {
return new ValidationBodyDefaultValueOK({
default: params.requestBody.default,
noDefault: params.requestBody.noDefault,
})
}

public async validationQuery(
params: ValidationQueryParams,
_context: SlushyContext<Context>,
Expand Down
1 change: 1 addition & 0 deletions server/src/middleware/RequestValidatorMiddlewareFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class RequestValidatorMiddlewareFactory implements MiddlewareFactory {
private readonly validator = new Ajv({
allErrors: true,
unknownFormats: 'ignore',
useDefaults: true,
})

public create(props: SlushyProps<any>, path?: string, operation?: PathHttpOperation): SlushyRequestHandler[] {
Expand Down

0 comments on commit 4aa1607

Please sign in to comment.