@lemonpeach/sequelize-to-json-schema

1.0.0 • Public • Published

sequelize-to-json-schema

Example

import Errors from '@feathersjs/errors';
import Ajv from 'ajv';
import { validateSchema } from 'feathers-hooks-common';
import jsonSchema from '@lemonpeach/sequelize-to-json-schema';

const getSchema = context => jsonSchema(
  context.service.Model,
  ['createdAt', 'updatedAt', 'id']
);

export const validateSchemaHook = async (
  context,
  schema = getSchema(context)
) => {
  try {
    switch (context.method) {
      case 'patch':
      case 'update':
        schema.required = [];
      /* eslint no-fallthrough: "error" */
      case 'create':
        await validateSchema(schema, new Ajv())(context);
        break;
      default:
        break;
    }
  } catch (error) {
   // handle error
  }
  return context;
};

Package Sidebar

Install

npm i @lemonpeach/sequelize-to-json-schema

Weekly Downloads

8

Version

1.0.0

License

MPL-2.0

Unpacked Size

70 kB

Total Files

21

Last publish

Collaborators

  • reyre
  • ayeo127