Based on sequelize-to-json-schemas
With following changes:
- Rewrite in TypeScript
- Added OpenAPI validations based on Sequelize validations
- Only OpenAPI is supported.
- Focused only on Sequelize 6
npm install --save @techntools/sequelize-to-openapi
import { SchemaManager, OpenApiStrategy } from '@techntools/sequelize-to-openapi'
const schemaManager = new SchemaManager
const strategy = new OpenApiStrategy
oapi.schema('User', schemaManager.generate(UserModel, strategy))
Pass (per) model options to the generate() method:
const userSchema = schemaManager.generate(userModel, strategy, {
exclude: ['someAttribute'],
include: ['someAttribute'],
associations: true,
excludeAssociations: ['someAssociation'],
includeAssociations: ['someAssociation'],
});
title
and description
are dropped
jsonSchema
and schema
works the same as sequelize-to-json-schemas
- GEOMETRY
- RANGE
- ABSTRACT
- GEOGRAPHY
- HSTORE
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
Following validators are supported:
Sequelize | OpenAPI Keyword |
---|---|
min | minimum |
max | maximum |
len | minLength/maxLength |
notEmpty | minLength |
notIn | { not: { enum: [] } } |
Sequelize | OpenAPI Format |
---|---|
isEmail | |
isUrl | url |
isUUID | uuid |
Sequelize | OpenAPI Pattern |
---|---|
isAlpha | ^[a-zA-Z]+$ |
isNumeric | ^[0-9]+$ |
isAlphanumeric | ^[a-zA-Z0-9]+$ |
isLowercase | ^[a-z]+$ |
isUppercase | ^[A-Z]+$ |
contains | ^.*' + val + '.*$ |
notContains | ^(?!.*' + val + ').*$ With array: ^(?!.*(' + val.args.join('|') + ')).*$ |
Sequelize | AJV |
---|---|
is as string |
{ regexp: '' } |
is as RegExp |
{ regexp: '' } |
is as { args: '', msg } |
{ regexp: '' } |
is as { args: RegExp, msg } |
{ regexp: '' } |
is as { args: ['pat', 'f'], msg } |
{ regexp: { pattern: pat, flag: f }} |
is as array |
{ regexp: { pattern: val[0], flag: val[1] }} |
not as string |
{ not: { regexp: '' }} |
not as RegExp |
{ not: { regexp: '' }} |
not as { args: '', msg } |
{ not: { regexp: '' }} |
not as { args: RegExp, msg } |
{ not: { regexp: '' }} |
not as { args: ['pat', 'f'], msg } |
{ not: { regexp: { pattern: pat, flag: f }}} |
not as array |
{ not: { regexp: { pattern: val[0], flag: val[1] }}} |
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 package for is
and not
validators.
This makes generated OpenAPI schema not fully compliant with the standard. But you can drop those validators if you face issues.
Check my repo for usage of the package. It uses sequelize-typescript.
Visit the OpenAPI documentation powered by scalar
express-openapi generates OpenAPI spec
This project is released under MIT LICENSE
- Keep the changes small
- Add the tests
- Existing tests have to pass
Full credits to the authors and contributors of sequelize-to-json-schemas for the great work