Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias schema.query to schema.querystring (#1690) #1694

Merged
merged 3 commits into from
Jun 9, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/Fluent-Schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const headersJsonSchema = S.object()

const schema = {
body: bodyJsonSchema.valueOf(),
querystring: queryStringJsonSchema.valueOf(),
querystring: queryStringJsonSchema.valueOf(), // (or) query: queryStringJsonSchema.valueOf()
params: paramsJsonSchema.valueOf(),
headers: headersJsonSchema.valueOf()
}
Expand Down
2 changes: 1 addition & 1 deletion docs/Validation-and-Serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Fastify uses a schema-based approach, and even if it is not mandatory we recomme
### Validation
The route validation internally relies upon [Ajv](https://www.npmjs.com/package/ajv), which is a high-performance JSON schema validator. Validating the input is very easy: just add the fields that you need inside the route schema, and you are done! The supported validations are:
- `body`: validates the body of the request if it is a POST or a PUT.
- `querystring`: validates the query string. This can be a complete JSON Schema object (with a `type` property of `'object'` and a `'properties'` object containing parameters) or a simpler variation in which the `type` and `properties` attributes are forgone and the query parameters are listed at the top level (see the example below).
- `querystring` or `query`: validates the query string. This can be a complete JSON Schema object (with a `type` property of `'object'` and a `'properties'` object containing parameters) or a simpler variation in which the `type` and `properties` attributes are forgone and the query parameters are listed at the top level (see the example below).
raghavmac marked this conversation as resolved.
Show resolved Hide resolved
- `params`: validates the route params.
- `headers`: validates the request headers.

Expand Down
5 changes: 5 additions & 0 deletions lib/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ Schemas.prototype.resolve = function (id) {
}

Schemas.prototype.resolveRefs = function (routeSchemas, dontClearId) {
// alias query to querystring schema
if (routeSchemas.query) {
routeSchemas.querystring = routeSchemas.query
raghavmac marked this conversation as resolved.
Show resolved Hide resolved
}

// let's check if our schemas have a custom prototype
for (const key of ['headers', 'querystring', 'params', 'body']) {
if (typeof routeSchemas[key] === 'object' && Object.getPrototypeOf(routeSchemas[key]) !== Object.prototype) {
Expand Down
31 changes: 31 additions & 0 deletions test/internals/validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,37 @@ test('build schema - payload schema', t => {
t.is(typeof opts[symbols.bodySchema], 'function')
})

test('build schema - query schema', t => {
t.plan(2)
const opts = {
schema: {
query: {
type: 'object',
properties: {
hello: { type: 'string' }
}
}
}
}
validation.build(opts, schema => ajv.compile(schema), new Schemas())
t.type(opts[symbols.querystringSchema].schema.type, 'string')
t.is(typeof opts[symbols.querystringSchema], 'function')
})

test('build schema - query schema abbreviated', t => {
t.plan(2)
const opts = {
schema: {
query: {
hello: { type: 'string' }
}
}
}
validation.build(opts, schema => ajv.compile(schema), new Schemas())
t.type(opts[symbols.querystringSchema].schema.type, 'string')
t.is(typeof opts[symbols.querystringSchema], 'function')
})

test('build schema - querystring schema', t => {
t.plan(2)
const opts = {
Expand Down