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

fix: type of validation function #4283

Merged
merged 9 commits into from Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 12 additions & 2 deletions docs/Reference/Request.md
Expand Up @@ -98,6 +98,9 @@ it will return a `validation` function that can be used to
validate diverse inputs. It returns `undefined` if no
serialization function was found using either of the provided inputs.

This function has property errors. Errors encountered during the last validation
are assigned to errors

```js
const validate = request
.getValidationFunction({
Expand All @@ -108,13 +111,15 @@ const validate = request
}
}
})
validate({ foo: 'bar' }) // true
console.log(validate({ foo: 'bar' })) // true
console.log(validate.errors) // undefined

// or

const validate = request
.getValidationFunction('body')
validate({ foo: 0.5 }) // false
console.log(validate({ foo: 0.5 })) // false
console.log(validate.errors) // validation errors
```

See [.compilaValidationSchema(schema, [httpStatus])](#compilevalidationschema)
Expand All @@ -133,6 +138,9 @@ The optional parameter `httpPart`, if provided, is forwarded directly
the `ValidationCompiler`, so it can be used to compile the validation
function if a custom `ValidationCompiler` is provided for the route.

This function has property errors. Errors encountered during the last validation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s add a small note just to indicate that this will only work this way of they’re using the default Fastify’s setup, if they’re tricking its own ValidationCompiler, most likely the behavior won’t be the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you arrange the text, please?

are assigned to errors

Uzlopak marked this conversation as resolved.
Show resolved Hide resolved

```js
const validate = request
Expand All @@ -145,6 +153,7 @@ const validate = request
}
})
console.log(validate({ foo: 'bar' })) // true
console.log(validate.errors) // undefined

// or

Expand All @@ -158,6 +167,7 @@ const validate = request
}
}, 200)
console.log(validate({ hello: 'world' })) // false
console.log(validate.errors) // validation errors
```

Note that you should be careful when using this function, as it will cache
Expand Down
12 changes: 9 additions & 3 deletions types/request.d.ts
@@ -1,3 +1,4 @@
import { ErrorObject } from '@fastify/ajv-compiler'
import { FastifyBaseLogger } from './logger'
import { ContextConfigDefault, RawServerBase, RawServerDefault, RawRequestDefaultExpression, RequestBodyDefault, RequestQuerystringDefault, RequestParamsDefault, RequestHeadersDefault } from './utils'
import { RouteGenericInterface } from './route'
Expand All @@ -14,6 +15,11 @@ export interface RequestGenericInterface {
Headers?: RequestHeadersDefault;
}

export interface ValidationFunction {
Eomm marked this conversation as resolved.
Show resolved Hide resolved
(input: any): boolean
errors?: null | ErrorObject[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we have also an unknown type for allowing inference maybe?
Just to match it with my prior point that devs can implement its own compiler which bifurcates from the default behaviour?

Copy link
Contributor Author

@budarin budarin Sep 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems to me that a separate issue with tests is needed here to account for this factor

In any case, I will not be able to handle this case (

}

/**
* FastifyRequest is an instance of the standard http or http2 request objects.
* It defaults to http.IncomingMessage, and it also extends the relative request object.
Expand Down Expand Up @@ -61,9 +67,9 @@ export interface FastifyRequest<RouteGeneric extends RouteGenericInterface = Rou
readonly is404: boolean;
readonly socket: RawRequest['socket'];

getValidationFunction(httpPart: HTTPRequestPart): (input: any) => boolean
getValidationFunction(schema: {[key: string]: any}): (input: any) => boolean
compileValidationSchema(schema: {[key: string]: any}, httpPart?: HTTPRequestPart): (input: any) => boolean
getValidationFunction(httpPart: HTTPRequestPart): ValidationFunction
getValidationFunction(schema: {[key: string]: any}): ValidationFunction
compileValidationSchema(schema: {[key: string]: any}, httpPart?: HTTPRequestPart): ValidationFunction
validateInput(input: any, schema: {[key: string]: any}, httpPart?: HTTPRequestPart): boolean
validateInput(input: any, httpPart?: HTTPRequestPart): boolean

Expand Down