Skip to content

Allow required, description, and enum to be regular fields if their types are wrong #51

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,13 @@ const mapSchemaTypeToFieldSchema = ({
const meta: any = {};

for (const metaProp of props) {
if (value && value[metaProp] != null) {
meta[metaProp] = value[metaProp];
const metaValue = value[metaProp];
const isValidEnum = metaProps === 'enum' ? Array.isArray(metaValue) : true;
const isValidRequired = metaProps === 'required' ? Array.isArray(metaValue) || typeof metaValue === 'boolean' : true;
const isValidDescription = metaProps === 'description' ? typeof metaValue === 'string' : true;
Copy link
Contributor

Choose a reason for hiding this comment

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

@JulianKingman Wouldn't a more succinct way to address validity of meta props be to check the non-existence of { type }? This is how mongoose requires overrides of meta props in schema definitions.

enum: '' // meta
enum: { type: something } // your field

const defaultSupportedMetaPropsAreValid = isValidDescription && isValidEnum && isValidRequired;
if (value && metaValue != null && defaultSupportedMetaPropsAreValid) {
meta[metaProp] = metaValue;
}
}

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-to-swagger",
"version": "1.5.0",
"version": "1.5.1",
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't bump patch versions for lib maintainers 🙏

"description": "Conversion library for transforming Mongoose schema objects into Swagger schema definitions.",
"homepage": "",
"author": {
Expand Down Expand Up @@ -44,7 +44,8 @@
"test:watch": "mocha lib/*.test.js --watch",
"test": "mocha lib/*.test.ts",
"build": "tsc",
"prepublish": "npm run build"
"prepublish": "npm run build",
"preinstall": "npm run build"
Comment on lines +47 to +48
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"prepublish": "npm run build",
"preinstall": "npm run build"
"prepublish": "npm run build"

},
"dependencies": {},
"license": "MIT",
Expand Down