Skip to content

Commit

Permalink
Initial version of the OpenApi schema-validation-wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
bravo-kernel committed Aug 16, 2019
1 parent 7bcbc48 commit 7595d7b
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 8 deletions.
26 changes: 22 additions & 4 deletions test-the-strategy-pattern.js
Expand Up @@ -11,6 +11,8 @@
// ============================================================================
const Sequelize = require('sequelize');

const { DataTypes } = Sequelize;

const sequelize = new Sequelize({
dialect: 'mysql'
});
Expand All @@ -28,15 +30,31 @@ const schemaManager = new SchemaManager({
baseUri: 'https://api.example.com',
});

// ----------------------------------
// Generate JSON Schema v6 schema
// ----------------------------------
const json6strategy = new JsonSchema6Strategy();
let schema = schemaManager.generate(userModel, json6strategy);
let userSchema = schemaManager.generate(userModel, json6strategy);

console.log('JSON Schema v6:')
console.log(schema);
console.log(userSchema);

// ----------------------------------
// Generate OpenAPI v3 schema
// ----------------------------------
const openapi3strategy = new OpenApi3Strategy();
schema = schemaManager.generate(userModel, openapi3strategy);
userSchema = schemaManager.generate(userModel, openapi3strategy);

console.log('OpenAPI v3:');
console.log(schema);
console.log(userSchema);

// OpenApi requires more than just the model schema for validation so we insert it into the wrapper
const validationSchema = require('./test/strategies/json-api-v3/schema-validation-wrapper');

validationSchema.components.schemas.users = userSchema;
console.log('Validation schema object:');
console.log(validationSchema);

console.log('Validation schema as JSON string:');
console.log(JSON.stringify(validationSchema, null, 2));

66 changes: 62 additions & 4 deletions test/models/user.js
@@ -1,3 +1,15 @@
/**
* Sequelize attribute definitions for the `user` model.
*
* This model contains ALL known Sequelize DataTypes and is used by all
* strategies to make sure the TypeMapper produces the correct types and
* thus produces a result that passed schema-validation for the strategy.
*
* @see https://sequelize.org/master/manual/data-types.html
*/

/* eslint-disable no-underscore-dangle */

const Sequelize = require('sequelize');

const { DataTypes } = Sequelize;
Expand All @@ -12,13 +24,43 @@ module.exports = sequelize => {
const Model = sequelize.define(
'user',
{
// id
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
},

// STRING (default)
_STRING: {
type: DataTypes.STRING,
allowNull: true, // nice test because this should become `nullable: true` for OpenApi
},

// STRING(1234)
_STRING_LENGTH: {
type: DataTypes.STRING(50),
},

// STRING.BINARY
_STRING_BINARY: {
type: DataTypes.STRING.BINARY,
},

// INTEGER (default)
_INTEGER: {
type: DataTypes.INTEGER,
defaultValue: 0,
},

// UUIDv4
_UUIDv4: {
type: DataTypes.UUID, // could be v1 or v4 ??
},

// ----------------------------------------------------------------------
// additions to sequelize datatypes, used to check overrides etc.
// ----------------------------------------------------------------------

// email
email: {
type: DataTypes.STRING,
Expand All @@ -39,6 +81,12 @@ module.exports = sequelize => {
type: Sequelize.STRING,
allowNull: false,
},
// hostAddress is a good design-driving test as the TypeMapper returned value (array) breaks OpenApi v3.
// The INET type holds an IPv4 or IPv6 host address, and optionally its subnet. Takes 7 or 19 bytes
hostAddress: {
type: Sequelize.INET,
allowNull: false,
},
},
// sequelize options
{
Expand All @@ -47,5 +95,15 @@ module.exports = sequelize => {
},
);

// dynamically add and remove attributes (useful for testing)
Model.rawAttributes._NEW = {
type: DataTypes.STRING,
allowNull: false,
};

delete Model.rawAttributes.email;

Model.refreshAttributes();

return Model;
};
42 changes: 42 additions & 0 deletions test/strategies/json-api-v3/schema-validation-wrapper.js
@@ -0,0 +1,42 @@
/**
* To validate OpenApi schemas we need more than just the schemas produced
* by sequelize-to-any-json-schema. This skeleton provides the surrounding
* schema required to pass validation (where `components/schemas` will be
* filled with the content provided by the OpenApi6Strategy produced schema.
*
* Please note that we also need to set the root key (e.g. `users`)
*/

module.exports = Object.freeze({
openapi: '3.0.2',
info: {
title: 'Fake API',
description: 'Dummy output for testing schemas generated by sequelize-to-any-json-schema',
version: '0.0.1',
},
paths: {
'/users': {
get: {
parameters: [],
responses: {
'404': {
description: 'not found',
},
},
description: 'Retrieves a list of all users from the API.',
summary: '',
tags: ['users'],
security: [],
},
},
},
components: {
schemas: {},
},
tags: [
{
name: 'users',
description: 'A users service',
},
],
});

0 comments on commit 7595d7b

Please sign in to comment.