diff --git a/package.json b/package.json index 6ecbf5c..0516149 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@luca-financial/luca-schema", - "version": "1.0.6", + "version": "1.0.7-beta.4", "description": "Schemas for the Luca Ledger application", "author": "Johnathan Aspinwall", "main": "dist/index.js", @@ -54,12 +54,7 @@ } }, "files": [ - "dist/cjs/index.js", - "dist/cjs/validators.js", - "dist/cjs/schemas/", - "dist/esm/index.js", - "dist/esm/validators.js", - "dist/esm/schemas/", + "dist/", "LICENSE", "README.md" ] diff --git a/scripts/buildEsm.cjs b/scripts/buildEsm.cjs index 9dd9fcf..8d13389 100644 --- a/scripts/buildEsm.cjs +++ b/scripts/buildEsm.cjs @@ -1,46 +1,58 @@ -const fs = require("fs-extra"); +const fs = require('fs-extra'); -const config = require("./config.cjs"); +const config = require('./config.cjs'); const { schemasSrc, + constantsSrc, indexSrc, - validatorsSrc, + validatorSrc, + esmConstantsDst, esmIndexDst, - esmValidatorsDst, - esmSchemasDst, + esmValidatorDst, + esmSchemasDst } = config; +const copyConstants = async () => { + return fs + .copy(constantsSrc, esmConstantsDst) + .then(() => console.log('Copy constants.js completed!')) + .catch(err => + console.error('An error occurred while copying constants.js', err) + ); +}; + const copyIndex = async () => { return fs .copy(indexSrc, esmIndexDst) - .then(() => console.log("Copy index.js completed!")) - .catch((err) => - console.error("An error occurred while copying index.js", err) + .then(() => console.log('Copy index.js completed!')) + .catch(err => + console.error('An error occurred while copying index.js', err) ); }; -const copyValidators = async () => { +const copyValidator = async () => { return fs - .copy(validatorsSrc, esmValidatorsDst) - .then(() => console.log("Copy validators.js completed!")) - .catch((err) => - console.error("An error occurred while copying validators.js", err) + .copy(validatorSrc, esmValidatorDst) + .then(() => console.log('Copy validators.js completed!')) + .catch(err => + console.error('An error occurred while copying validators.js', err) ); }; const copySchemas = async () => { return fs .copy(schemasSrc, esmSchemasDst, { overwrite: true }) - .then(() => console.log("Copy schemas/ to cjs/ completed!")) - .catch((err) => - console.error("An error occurred while copying the schemas/ folder.", err) + .then(() => console.log('Copy schemas/ to cjs/ completed!')) + .catch(err => + console.error('An error occurred while copying the schemas/ folder.', err) ); }; const buildEsm = async () => { + await copyConstants(); await copyIndex(); - await copyValidators(); + await copyValidator(); await copySchemas(); }; diff --git a/scripts/config.cjs b/scripts/config.cjs index 81a840e..9ea0153 100644 --- a/scripts/config.cjs +++ b/scripts/config.cjs @@ -1,21 +1,23 @@ -const path = require("path"); +const path = require('path'); -const srcPath = path.join(__dirname, "..", "src"); -const examplesSrc = path.join(srcPath, "examples"); -const indexSrc = path.join(srcPath, "index.js"); -const schemasSrc = path.join(srcPath, "schemas"); -const validatorsSrc = path.join(srcPath, "validators.js"); +const srcPath = path.join(__dirname, '..', 'src'); +const constantsSrc = path.join(srcPath, 'constants.js'); +const examplesSrc = path.join(srcPath, 'examples'); +const indexSrc = path.join(srcPath, 'index.js'); +const schemasSrc = path.join(srcPath, 'schemas'); +const validatorSrc = path.join(srcPath, 'lucaValidator.js'); -const distPath = path.join(__dirname, "..", "dist"); +const distPath = path.join(__dirname, '..', 'dist'); -const cjsPath = path.join(distPath, "cjs"); -const cjsExamplesDst = path.join(cjsPath, "examples"); -const cjsSchemasDst = path.join(cjsPath, "schemas"); +const cjsPath = path.join(distPath, 'cjs'); +const cjsExamplesDst = path.join(cjsPath, 'examples'); +const cjsSchemasDst = path.join(cjsPath, 'schemas'); -const esmPath = path.join(distPath, "esm"); -const esmIndexDst = path.join(esmPath, "index.js"); -const esmSchemasDst = path.join(esmPath, "schemas"); -const esmValidatorsDst = path.join(esmPath, "validators.js"); +const esmPath = path.join(distPath, 'esm'); +const esmConstantsDst = path.join(esmPath, 'constants.js'); +const esmIndexDst = path.join(esmPath, 'index.js'); +const esmSchemasDst = path.join(esmPath, 'schemas'); +const esmValidatorDst = path.join(esmPath, 'lucaValidator.js'); module.exports = { distPath, @@ -23,12 +25,14 @@ module.exports = { esmPath, cjsExamplesDst, cjsSchemasDst, + esmConstantsDst, esmIndexDst, - esmValidatorsDst, + esmValidatorDst, esmSchemasDst, srcPath, + constantsSrc, examplesSrc, indexSrc, schemasSrc, - validatorsSrc, + validatorSrc }; diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 0000000..658dcee --- /dev/null +++ b/src/constants.js @@ -0,0 +1,67 @@ +const TransactionStateEnum = Object.freeze({ + PLANNED: 'PLANNED', + SCHEDULED: 'SCHEDULED', + PENDING: 'PENDING', + COMPLETED: 'COMPLETED', + CANCELLED: 'CANCELLED', + FAILED: 'FAILED', + DISPUTED: 'DISPUTED', + REFUNDED: 'REFUNDED', + TENTATIVE: 'TENTATIVE', + UPCOMING: 'UPCOMING', + DELETED: 'DELETED' +}); + +const CategoryTypeEnum = Object.freeze({ + DEFAULT: 'DEFAULT', + MODIFIED: 'MODIFIED', + CUSTOM: 'CUSTOM' +}); + +const EntityTypeEnum = Object.freeze({ + ACCOUNT: 'ACCOUNT', + RETAILER: 'RETAILER', + BUSINESS: 'BUSINESS', + INDIVIDUAL: 'INDIVIDUAL', + UTILITY: 'UTILITY', + GOVERNMENT: 'GOVERNMENT' +}); + +const EntityStatusEnum = Object.freeze({ + ACTIVE: 'ACTIVE', + INACTIVE: 'INACTIVE', + SUSPENDED: 'SUSPENDED', + DELETED: 'DELETED', + CLOSED: 'CLOSED' +}); + +const RepeatedTransactionFrequencyEnum = Object.freeze({ + DAY: 'DAY', + WEEK: 'WEEK', + MONTH: 'MONTH', + YEAR: 'YEAR' +}); + +const RepeatedTransactionStateEnum = Object.freeze({ + ACTIVE: 'ACTIVE', + PAUSED: 'PAUSED', + COMPLETED: 'COMPLETED', + CANCELLED: 'CANCELLED' +}); + +const RepeatedTransactionOccurrenceStatusEnum = Object.freeze({ + MODIFIED: 'MODIFIED', + DELETED: 'DELETED' +}); + +const constants = { + TransactionStateEnum, + CategoryTypeEnum, + EntityTypeEnum, + EntityStatusEnum, + RepeatedTransactionFrequencyEnum, + RepeatedTransactionStateEnum, + RepeatedTransactionOccurrenceStatusEnum +}; + +export default constants; diff --git a/src/index.js b/src/index.js index 3e40b64..3d5ecb4 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ +import constants from './constants'; +import lucaValidator from './lucaValidator'; import schemas from './schemas'; -import validators from './validators'; -export { schemas, validators }; +export { constants, lucaValidator, schemas }; diff --git a/src/lucaValidator.js b/src/lucaValidator.js new file mode 100644 index 0000000..b52c2cc --- /dev/null +++ b/src/lucaValidator.js @@ -0,0 +1,18 @@ +import Ajv2020 from 'ajv/dist/2020'; +import addFormats from 'ajv-formats'; + +import schemas from './schemas'; + +const lucaValidator = new Ajv2020(); +addFormats(lucaValidator); + +Object.entries(schemas).forEach(([key, schema]) => { + if (!lucaValidator.validateSchema(schema)) { + console.error(`Invalid schema: ${key}`); + console.error(lucaValidator.errors); + } else { + lucaValidator.addSchema(schema, key); + } +}); + +export default lucaValidator; diff --git a/src/tests/categories.test.js b/src/tests/categories.test.js index 279f563..a846fed 100644 --- a/src/tests/categories.test.js +++ b/src/tests/categories.test.js @@ -1,7 +1,7 @@ import exampleData from '../examples/categories.json'; -import validators from '../validators'; +import { lucaValidator } from '../'; -const { validateCategory } = validators; +const validateCategory = lucaValidator.getSchema('category'); test('examples are valid categories', () => { exampleData.forEach(example => { diff --git a/src/tests/entities.test.js b/src/tests/entities.test.js index e4bbe16..a57d9b6 100644 --- a/src/tests/entities.test.js +++ b/src/tests/entities.test.js @@ -1,7 +1,7 @@ import exampleData from '../examples/entities.json'; -import validators from '../validators'; +import { lucaValidator } from '../'; -const { validateEntity } = validators; +const validateEntity = lucaValidator.getSchema('entity'); test('examples are valid entities', () => { exampleData.forEach(example => { diff --git a/src/tests/lucaSchema.test.js b/src/tests/lucaSchema.test.js index ca8bd84..651358f 100644 --- a/src/tests/lucaSchema.test.js +++ b/src/tests/lucaSchema.test.js @@ -1,7 +1,7 @@ import exampleData from '../examples/lucaSchema.json'; -import validators from '../validators'; +import { lucaValidator } from '../'; -const { validateLucaSchema } = validators; +const validateLucaSchema = lucaValidator.getSchema('lucaSchema'); test('full luca schema object is valid', () => { const valid = validateLucaSchema(exampleData); diff --git a/src/tests/recurringTransactionEvents.test.js b/src/tests/recurringTransactionEvents.test.js index 7a9948a..6a5b081 100644 --- a/src/tests/recurringTransactionEvents.test.js +++ b/src/tests/recurringTransactionEvents.test.js @@ -1,7 +1,7 @@ import exampleData from '../examples/recurringTransactionEvents.json'; -import validators from '../validators'; +import { lucaValidator } from '../'; -const { validateRecurringTransactionEvent } = validators; +const validateRecurringTransactionEvent = lucaValidator.getSchema('recurringTransactionEvent'); test('examples are valid recurringTransactionEvents', () => { exampleData.forEach(example => { diff --git a/src/tests/recurringTransactions.test.js b/src/tests/recurringTransactions.test.js index 4874bc0..32f9c41 100644 --- a/src/tests/recurringTransactions.test.js +++ b/src/tests/recurringTransactions.test.js @@ -1,7 +1,7 @@ import exampleData from '../examples/recurringTransactions.json'; -import validators from '../validators'; +import { lucaValidator } from '../'; -const { validateRecurringTransaction } = validators; +const validateRecurringTransaction = lucaValidator.getSchema('recurringTransaction'); test('examples are valid recurringTransactions', () => { exampleData.forEach(example => { diff --git a/src/tests/schemas.test.js b/src/tests/schemas.test.js index 5cd41dd..7876d23 100644 --- a/src/tests/schemas.test.js +++ b/src/tests/schemas.test.js @@ -1,7 +1,7 @@ import exampleData from '../examples/schemas.json'; -import validators from '../validators'; +import { lucaValidator } from '../'; -const { validateSchema } = validators; +const validateSchema = lucaValidator.getSchema('schema'); test('examples are valid schema objects', () => { exampleData.forEach(example => { diff --git a/src/tests/transactions.test.js b/src/tests/transactions.test.js index 4e0e68b..098bc24 100644 --- a/src/tests/transactions.test.js +++ b/src/tests/transactions.test.js @@ -1,7 +1,7 @@ import exampleData from '../examples/transactions.json'; -import validators from '../validators'; +import { lucaValidator } from '../'; -const { validateTransaction } = validators; +const validateTransaction = lucaValidator.getSchema('transaction'); test('examples are valid transactions', () => { exampleData.forEach(example => { diff --git a/src/validators.js b/src/validators.js deleted file mode 100644 index 4940fb9..0000000 --- a/src/validators.js +++ /dev/null @@ -1,28 +0,0 @@ -import Ajv2020 from 'ajv/dist/2020'; -import addFormats from 'ajv-formats'; - -import schemas from './schemas'; - -const ajv = new Ajv2020(); -addFormats(ajv); - -Object.entries(schemas).forEach(([key, schema]) => { - if (!ajv.validateSchema(schema)) { - console.error(`Invalid schema: ${key}`); - console.error(ajv.errors); - } else { - ajv.addSchema(schema, key); - } -}); - -const validators = { - validateCategory: ajv.getSchema('category'), - validateEntity: ajv.getSchema('entity'), - validateLucaSchema: ajv.getSchema('lucaSchema'), - validateRecurringTransaction: ajv.getSchema('recurringTransaction'), - validateRecurringTransactionEvent: ajv.getSchema('recurringTransactionEvent'), - validateSchema: ajv.getSchema('schema'), - validateTransaction: ajv.getSchema('transaction') -}; - -export default validators;