Skip to content
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
34 changes: 16 additions & 18 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ language: node_js
cache: npm

node_js:
- 8
- 10
- 12
- 14
- 15
- 16
- 17

sudo: false

Expand Down Expand Up @@ -34,21 +32,21 @@ matrix:
- node_js: 14
env: TEST=0 COVERAGE=1

- stage: deploy
node_js: 15
env: TEST=0 COVERAGE=0 LINT=0
script:
- echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc
- git remote rm origin
- git remote add origin https://alt3:${GITHUB_TOKEN}@github.com/alt3/sequelize-to-json-schemas.git
- git symbolic-ref HEAD refs/heads/master
- npm run release
on:
branch: master
if: "branch = master AND type = push AND commit_message !~ /(?i:no-release)|^(?i:chore: release)/"
# - stage: deploy
# node_js: 17
# env: TEST=0 COVERAGE=0 LINT=0
# script:
# - echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc
# - git remote rm origin
# - git remote add origin https://alt3:${GITHUB_TOKEN}@github.com/alt3/sequelize-to-json-schemas.git
# - git symbolic-ref HEAD refs/heads/master
# - npm run release
# on:
# branch: master
# if: "branch = master AND type = push AND commit_message !~ /(?i:no-release)|^(?i:chore: release)/"

allow_failures:
- env: SEQUELIZE_VERSION=6
# allow_failures:
# - env: SEQUELIZE_VERSION=6

before_script:
- sh -c "if [ '$TEST' = '1' ]; then npm install sequelize@$SEQUELIZE_VERSION; fi"
Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
Convert Sequelize models into these JSON Schema variants (using the Strategy Pattern):

- JSON Schema Draft-07 - [sample output](examples/json-schema-v7.md)
- JSON Schema 2019-09 - [sample output](examples/json-schema-v2019-09.md)
- OpenAPI 3.0 - [sample output](examples/openapi-v3.md)

Compatible with Sequelize versions 4, 5 and 6.
Compatible with:

- Sequelize versions 4, 5 and 6
- NodeJS version 14, 16 and 17

## Main Goals

Expand All @@ -38,10 +42,13 @@ npm install @alt3/sequelize-to-json-schemas --save
const { JsonSchemaManager, JsonSchema7Strategy, OpenApi3Strategy } = require('@alt3/sequelize-to-json-schemas');
const schemaManager = new JsonSchemaManager();

// now generate a JSON Schema Draft-07 model schema
// to generate a JSON Schema Draft-07 schema for your model:
let schema = schemaManager.generate(userModel, new JsonSchema7Strategy());

// to generate a JSON Schema 2019-09 schema:
let schema = schemaManager.generate(userModel, new JsonSchema7Strategy());

// and/or the OpenAPI 3.0 equivalent
// to generate the OpenAPI 3.0 equivalent
schema = schemaManager.generate(userModel, new OpenApi3Strategy());
```
<!-- prettier-ignore-end -->
Expand All @@ -67,6 +74,7 @@ const userSchema = schemaManager.generate(userModel, strategy, {
description: 'Custom model description',
exclude: ['someAttribute'],
include: ['someAttribute'],
renderIdProperty: false, // true to render '$id' field for each schema property
associations: true,
excludeAssociations: ['someAssociation'],
includeAssociations: ['someAssociation'],
Expand Down
100 changes: 99 additions & 1 deletion examples/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
const fileSystem = require('fs');
const moment = require('moment');
const models = require('../test/models');
const { JsonSchemaManager, JsonSchema7Strategy, OpenApi3Strategy } = require('../lib');
const {
JsonSchemaManager,
JsonSchema7Strategy,
JsonSchema201909Strategy,
OpenApi3Strategy,
} = require('../lib');

const targetFolder = './examples/';

Expand Down Expand Up @@ -118,6 +123,99 @@ fileSystem.writeFile(`${targetFolder}json-schema-v7.md`, markdown, function chec

console.log('Succesfully generated markdown sample output for JSON Schema Draft-07');

// ----------------------------------------------------------------------------
// Json Schema 2019-09
// ----------------------------------------------------------------------------
strategy = new JsonSchema201909Strategy();

userSchema = schemaManager.generate(models.user, strategy, {
title: 'Custom User Title',
description: 'Custom User Description',
});

profileSchema = schemaManager.generate(models.profile, strategy);
documentSchema = schemaManager.generate(models.document, strategy);
companySchema = schemaManager.generate(models.company, strategy);
friendshipSchema = schemaManager.generate(models.friendship, strategy);

fullSchema = {
$schema: 'https://json-schema.org/draft-07/schema#',
definitions: {
user: userSchema,
profile: profileSchema,
document: documentSchema,
company: companySchema,
friendship: friendshipSchema,
},
};

markdown = `# JSON Schema Draft 2019-09
${pageIntro}

- [JSON Schema Validator](https://www.jsonschemavalidator.net/)
- [ajv](https://github.com/epoberezkin/ajv)

## User Model

<!-- prettier-ignore-start -->
\`\`\`json
${JSON.stringify(userSchema, null, 2)}
\`\`\`
<!-- prettier-ignore-end -->

## Profile Model

<!-- prettier-ignore-start -->
\`\`\`json
${JSON.stringify(profileSchema, null, 2)}
\`\`\`
<!-- prettier-ignore-end -->

## Document Model

<!-- prettier-ignore-start -->
\`\`\`json
${JSON.stringify(documentSchema, null, 2)}
\`\`\`
<!-- prettier-ignore-end -->

## Company Model

<!-- prettier-ignore-start -->
\`\`\`json
${JSON.stringify(companySchema, null, 2)}
\`\`\`
<!-- prettier-ignore-end -->

## Friendship Model

<!-- prettier-ignore-start -->
\`\`\`json
${JSON.stringify(friendshipSchema, null, 2)}
\`\`\`
<!-- prettier-ignore-end -->

## Full Schema

Please note that sequelize-to-json-schemas does NOT generate full schemas. This is just an
example of how to integrate the generated model schemas into a full JSON Schema Draft 2019-09
document (by adding model schemas to \`definitions\`).

<!-- prettier-ignore-start -->
\`\`\`json
${JSON.stringify(fullSchema, null, 2)}
\`\`\`
<!-- prettier-ignore-end -->
`;

fileSystem.writeFile(`${targetFolder}json-schema-v2019-09.md`, markdown, function check(error) {
if (error) {
throw error;
}
});

console.log('Succesfully generated markdown sample output for JSON Schema Draft 2019-09');

// ----------------------------------------------------------------------------
// OpenAPI 3.0
// ----------------------------------------------------------------------------
Expand Down
Loading