|
| 1 | +## Sequelize To OpenAPI |
| 2 | + |
| 3 | +Based on [sequelize-to-json-schemas](https://github.com/alt3/sequelize-to-json-schemas) |
| 4 | + |
| 5 | +With following changes: |
| 6 | + |
| 7 | +1. Rewrite in TypeScript |
| 8 | +2. Added OpenAPI validations based on Sequelize validations |
| 9 | +3. Only OpenAPI is supported. |
| 10 | +4. Focused only on Sequelize 6 |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +```bash |
| 15 | +npm install --save @techntools/sequelize-to-openapi |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +```typescript |
| 21 | +import { SchemaManager, OpenApiStrategy } from '@techntools/sequelize-to-openapi' |
| 22 | + |
| 23 | +const schemaManager = new SchemaManager |
| 24 | +const strategy = new OpenApiStrategy |
| 25 | + |
| 26 | +oapi.schema('User', schemaManager.generate(UserModel, strategy)) |
| 27 | +``` |
| 28 | + |
| 29 | +## Configuration Options |
| 30 | + |
| 31 | +Pass (per) model options to the generate() method: |
| 32 | + |
| 33 | +```typescript |
| 34 | +const userSchema = schemaManager.generate(userModel, strategy, { |
| 35 | + exclude: ['someAttribute'], |
| 36 | + include: ['someAttribute'], |
| 37 | + associations: true, |
| 38 | + excludeAssociations: ['someAssociation'], |
| 39 | + includeAssociations: ['someAssociation'], |
| 40 | +}); |
| 41 | +``` |
| 42 | + |
| 43 | +`title` and `description` are dropped |
| 44 | + |
| 45 | +`jsonSchema` and `schema` works the same as sequelize-to-json-schemas |
| 46 | + |
| 47 | +## Unsupported Types |
| 48 | + |
| 49 | +* GEOMETRY |
| 50 | +* RANGE |
| 51 | +* ABSTRACT |
| 52 | +* GEOGRAPHY |
| 53 | +* HSTORE |
| 54 | + |
| 55 | +While sequelize-to-json-schemas throws error for these, this package simply ignores them so that you can use generated schema for rest of the types and support these types the way you see fit |
| 56 | + |
| 57 | +## Validators |
| 58 | + |
| 59 | +Following validators are supported: |
| 60 | + |
| 61 | +| Sequelize | OpenAPI Keyword | |
| 62 | +| :-------- | :-------------------------- | |
| 63 | +| min | minimum | |
| 64 | +| max | maximum | |
| 65 | +| len | minLength/maxLength | |
| 66 | +| notEmpty | minLength | |
| 67 | +| notIn | `{ not: { enum: [] } }`<br> | |
| 68 | + |
| 69 | +| Sequelize | OpenAPI Format | |
| 70 | +| :-------- | :------------- | |
| 71 | +| isEmail | email | |
| 72 | +| isUrl | url | |
| 73 | +| isUUID | uuid | |
| 74 | + |
| 75 | +| Sequelize | OpenAPI Pattern | |
| 76 | +| :------------- | :------------------------------------------------------------------------------------------ | |
| 77 | +| isAlpha | `^[a-zA-Z]+$` | |
| 78 | +| isNumeric | `^[0-9]+$` | |
| 79 | +| isAlphanumeric | `^[a-zA-Z0-9]+$` | |
| 80 | +| isLowercase | `^[a-z]+$` | |
| 81 | +| isUppercase | `^[A-Z]+$` | |
| 82 | +| contains | `^.*' + val + '.*$` | |
| 83 | +| notContains | `^(?!.*' + val + ').*$`<br><br>With array:<br><br>`^(?!.*(' + val.args.join('\|') + ')).*$` | |
| 84 | + |
| 85 | +| Sequelize | AJV | |
| 86 | +| -------------------------------------- | ------------------------------------------------------ | |
| 87 | +| `is` as string | `{ regexp: '' }` | |
| 88 | +| `is` as RegExp | `{ regexp: '' }` | |
| 89 | +| `is` as `{ args: '', msg }` | `{ regexp: '' }` | |
| 90 | +| `is` as `{ args: RegExp, msg }` | `{ regexp: '' }` | |
| 91 | +| `is` as `{ args: ['pat', 'f'], msg }` | `{ regexp: { pattern: pat, flag: f }}` | |
| 92 | +| `is` as array | `{ regexp: { pattern: val[0], flag: val[1] }}` | |
| 93 | +| `not` as string | `{ not: { regexp: '' }}` | |
| 94 | +| `not` as RegExp | `{ not: { regexp: '' }}` | |
| 95 | +| `not` as `{ args: '', msg }` | `{ not: { regexp: '' }}` | |
| 96 | +| `not` as `{ args: RegExp, msg }` | `{ not: { regexp: '' }}` | |
| 97 | +| `not` as `{ args: ['pat', 'f'], msg }` | `{ not: { regexp: { pattern: pat, flag: f }}}` | |
| 98 | +| `not` as array | `{ not: { regexp: { pattern: val[0], flag: val[1] }}}` | |
| 99 | + |
| 100 | +## Case with regular expression |
| 101 | + |
| 102 | +Flags such as i, g etc. are not supported in OpenAPI. Sequelize can use string or RegExp class for regex. So, to avoid these limitations, I have used `regexp` keyword from [ajv-keywords](https://github.com/ajv-validator/ajv-keywords) package for `is` and `not` validators. |
| 103 | + |
| 104 | +This makes generated OpenAPI schema not fully compliant with the standard. But you can drop those validators if you face issues. |
| 105 | + |
| 106 | +## Demo |
| 107 | + |
| 108 | +Check my [repo](https://github.com/techntools/sequelize-to-openapi-demo) for usage of the package. It uses sequelize-typescript. |
| 109 | + |
| 110 | +Visit the OpenAPI documentation powered by [scalar](https://www.scalar.com/) |
| 111 | + |
| 112 | +[express-openapi](https://github.com/wesleytodd/express-openapi) generates OpenAPI spec |
| 113 | + |
| 114 | +## License |
| 115 | + |
| 116 | +This project is released under [MIT LICENSE](https://github.com/techntools/sequelize-to-openapi/blob/master/LICENSE.txt) |
| 117 | + |
| 118 | +## Contributing |
| 119 | + |
| 120 | +1. Keep the changes small |
| 121 | +2. Add the tests |
| 122 | +3. Existing tests have to pass |
| 123 | + |
| 124 | +## Credits |
| 125 | + |
| 126 | +Full credits to the authors and contributors of [sequelize-to-json-schemas](https://github.com/alt3/sequelize-to-json-schemas) for the great work |
0 commit comments