-
-
Notifications
You must be signed in to change notification settings - Fork 209
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
transform joi to json not working with latest fastify #373
Comments
Was this working in v3.12.0? cc @Eomm |
just tested some version. seems to break since 3.0. we need 3.0 can't use 2.0. schema complier changed, so this might also change the schema :/ v2 in fastify docs: const joi = require('joi')
// Validation options to match ajv's baseline options used in Fastify
const joiOptions = {
abortEarly: false, // return all errors
convert: true, // change data type of data to match type keyword
allowUnknown : false, // remove additional properties
noDefaults: false
}
const joiBodySchema = joi.object().keys({
age: joi.number().integer().required(),
sub: joi.object().keys({
name: joi.string().required()
}).required()
})
const joiSchemaCompiler = schema => data => {
// joi `validate` function returns an object with an error property (if validation failed) and a value property (always present, coerced value if validation was successful)
const { error, value } = joiSchema.validate(data, joiOptions)
if (error) {
return { error }
} else {
return { value }
}
}
// or more simply...
const joiSchemaCompiler = schema => data => joiSchema.validate(data, joiOptions)
fastify.post('/the/url', {
schema: {
body: joiBodySchema
},
schemaCompiler: joiSchemaCompiler
}, handler) v3 in fastify docs: const Joi = require('@hapi/joi')
fastify.post('/the/url', {
schema: {
body: Joi.object().keys({
hello: Joi.string().required()
}).required()
},
validatorCompiler: ({ schema, method, url, httpPart }) => {
return data => schema.validate(data)
}
}, handler) |
the schema the transform functions get are following { hide: true, [Symbol(fastify.schemas.visited)]: true }
{ hide: true, [Symbol(fastify.schemas.visited)]: true }
{ hide: true, [Symbol(fastify.schemas.visited)]: true }
{ hide: true, [Symbol(fastify.schemas.visited)]: true }
{ hide: true, [Symbol(fastify.schemas.visited)]: true }
{ hide: true, [Symbol(fastify.schemas.visited)]: true }
{
body: {
type: 'object',
'$_root': {
_types: [Set],
alternatives: [Function (anonymous)],
any: [Function (anonymous)],
array: [Function (anonymous)],
boolean: [Function (anonymous)],
date: [Function (anonymous)],
function: [Function (anonymous)],
link: [Function (anonymous)],
number: [Function (anonymous)],
object: [Function (anonymous)],
string: [Function (anonymous)],
symbol: [Function (anonymous)],
binary: [Function (anonymous)],
allow: [Function (anonymous)],
custom: [Function (anonymous)],
disallow: [Function (anonymous)],
equal: [Function (anonymous)],
exist: [Function (anonymous)],
forbidden: [Function (anonymous)],
invalid: [Function (anonymous)],
not: [Function (anonymous)],
only: [Function (anonymous)],
optional: [Function (anonymous)],
options: [Function (anonymous)],
prefs: [Function (anonymous)],
preferences: [Function (anonymous)],
required: [Function (anonymous)],
strip: [Function (anonymous)],
valid: [Function (anonymous)],
when: [Function (anonymous)],
ValidationError: [class (anonymous) extends Error],
version: '17.4.0',
cache: [Object],
assert: [Function: assert],
attempt: [Function: attempt],
build: [Function: build],
checkPreferences: [Function: checkPreferences],
compile: [Function: compile],
defaults: [Function: defaults],
expression: [Function: expression],
extend: [Function: extend],
isError: [Function: isError],
isExpression: [Function: isTemplate],
isRef: [Function (anonymous)],
isSchema: [Function (anonymous)],
in: [Function: in],
override: Symbol(override),
ref: [Function: ref],
types: [Function: types],
alt: [Function (anonymous)],
bool: [Function (anonymous)],
func: [Function (anonymous)],
x: [Function: expression],
trace: [Function: trace],
untrace: [Function (anonymous)],
[Symbol(@hapi/lab/coverage/initialize)]: [Function: trace]
},
'$_temp': { ruleset: false, whens: {} },
_ids: { _byId: Map(0) {}, _byKey: [Map], _schemaChain: false },
_preferences: null,
_valids: null,
_invalids: null,
_rules: [],
_singleRules: Map(0) {},
_refs: { refs: [] },
_flags: { presence: 'required' },
_cache: null,
'$_terms': {
alterations: null,
examples: null,
externals: null,
metas: [],
notes: [],
shared: null,
tags: [],
whens: null,
dependencies: null,
keys: [Array],
patterns: null,
renames: null
},
'$_super': { default: [Function: bound default] }
}
} |
A PR to fix this would be highly welcomed. |
Here is what we support: https://github.com/fastify/fastify-swagger/blob/master/test/transform.js. |
@mcollina It should work, as the schema looks good.! do you know what this is, and where there are coming from?? { hide: true, [Symbol(fastify.schemas.visited)]: true } |
🐛 Bug Report
A clear and concise description of what the bug is.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
A clear and concise description of what you expected to happen.
Actual behavior
Your Environment
The text was updated successfully, but these errors were encountered: