kogosoftwarellc / open-api

A Monorepo of various packages to power OpenAPI in node
MIT License
891 stars 234 forks source link

Nullable not fully supported - Ajv Dependency initialized without "nullable: true" #614

Open markyme21 opened 4 years ago

markyme21 commented 4 years ago

Looks like Ajv is initialized in Request/Response validators (Needed them both, may be relevant to others) without "nulllable" set to TRUE, causing validation errors.

var v = new Ajv({ ..., nullable: true });

This fixed the issue I was having.

I'm using express-openapi@6.0.0 which uses openapi-framework@0.26.0 -> "openapi-request-validator": "4.2.0" and "openapi-response-validator": "4.0.0".

Was trying to use OPENAPI3 JSON definition which looks like:

"SomeReferenceObjectArray": { "type": "array", "items": {"$ref": "#/components/schemas/SomeReferenceObject"} }, "SomeReferenceObject": { "type": "object", "properties": { "thisismyfield": { "nullable": true, "type": "string" } } }

jsdevel commented 4 years ago

@markyme21 what happens if nullable: false is set on a schema item, but globally in AJV we allow nullable?

markyme21 commented 4 years ago

@jsdevel Same as happens now, an error.

Ajv -> validate.js if ($typeSchema && it.opts.nullable && it.schema.nullable === true) { if ($typeIsArray) { if ($typeSchema.indexOf('null') == -1) $typeSchema = $typeSchema.concat('null'); } else if ($typeSchema != 'null') { typeSchema = [$typeSchema, 'null']; $typeIsArray = true; } }

$typeSchema which then used to compile the validation code will stay the same. Right? 👨‍💻

jsdevel commented 4 years ago

not sure. feel free to open a PR. ensure that whatever you do adheres to the spec as much as possible.