Skip to content

Commit

Permalink
Refactor schemas: use CaseInsensitiveSchema constructor
Browse files Browse the repository at this point in the history
Instead tons of redundant renames in schemas, use CaseInsensitiveSchema constructor to convert schemas automatically.
  • Loading branch information
aron123 committed Aug 24, 2019
1 parent 16a3691 commit 3ed8738
Show file tree
Hide file tree
Showing 19 changed files with 116 additions and 149 deletions.
12 changes: 2 additions & 10 deletions lib/build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { immutableFields } = require('./constants');
const { sanitizeThenValidate } = require('./validate');
const { setFieldsForbidden } = require('./schema');

/**
* Does a case-insensitive search. Returns if the value is presented in the given array.
Expand Down Expand Up @@ -62,15 +63,6 @@ function buildRequestWithoutValidation (schema, defaults, customs) {
return request;
}

/**
* Makes the given keys forbidden in the given Joi-schema.
* @param {Object} schema - The Joi-schema to modify.
* @param {String[]} forbiddenKeys - The forbidden keys.
*/
function setKeysForbidden (schema, forbiddenKeys) {
return schema.fork(forbiddenKeys, key => key.forbidden());
}

/**
* Builds request object, that could send to the Barion API.
* @param {Object} schema - Schema, which defines structure of the request object.
Expand All @@ -80,7 +72,7 @@ function setKeysForbidden (schema, forbiddenKeys) {
function buildRequest (schema, defaults, options) {
const schemaFields = Object.keys(schema.describe().keys);
const immutablesInSchema = getCaseInsensitiveIntersection(schemaFields, immutableFields);
const validationSchema = (immutablesInSchema.length > 0) ? setKeysForbidden(schema, immutablesInSchema) : schema;
const validationSchema = (immutablesInSchema.length > 0) ? setFieldsForbidden(schema, immutablesInSchema) : schema;

const customs = sanitizeThenValidate(validationSchema, options, true);
const merged = Object.assign({}, defaults, customs);
Expand Down
13 changes: 4 additions & 9 deletions lib/domain/BankTransfer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const Joi = require('@hapi/joi');
const { CaseInsensitiveSchema } = require('../schema');

const BankAccount = require('./common/BankAccount');
const constraints = require('./_constraints');

Expand All @@ -19,13 +21,6 @@ const schema = Joi.object({
Comment: Joi.string().optional()
.max(90),
BankAccount: BankAccount.required()
})
.rename(/^UserName$/i, 'UserName', { override: true })
.rename(/^Password$/i, 'Password', { override: true })
.rename(/^Currency$/i, 'Currency', { override: true })
.rename(/^Amount$/i, 'Amount', { override: true })
.rename(/^RecipientName$/i, 'RecipientName', { override: true })
.rename(/^Comment$/i, 'Comment', { override: true })
.rename(/^BankAccount$/i, 'BankAccount', { override: true });
});

module.exports = schema;
module.exports = new CaseInsensitiveSchema(schema);
9 changes: 4 additions & 5 deletions lib/domain/FinishReservation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const Joi = require('@hapi/joi');
const { CaseInsensitiveSchema } = require('../schema');

const TransactionToFinish = require('./common/TransactionToFinish');

/**
Expand All @@ -14,9 +16,6 @@ const schema = Joi.object({
Transactions: Joi.array().required()
.items(TransactionToFinish)
.min(1)
})
.rename(/^POSKey$/i, 'POSKey', { override: true })
.rename(/^PaymentId$/i, 'PaymentId', { override: true })
.rename(/^Transactions$/i, 'Transactions', { override: true });
});

module.exports = schema;
module.exports = new CaseInsensitiveSchema(schema);
7 changes: 3 additions & 4 deletions lib/domain/GetPaymentState.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Joi = require('@hapi/joi');
const { CaseInsensitiveSchema } = require('../schema');

/**
* Used to query the details and current state of a given payment.
Expand All @@ -10,8 +11,6 @@ const schema = Joi.object({
.guid(),
PaymentId: Joi.string().required()
.guid()
})
.rename(/^POSKey$/i, 'POSKey', { override: true })
.rename(/^PaymentId$/i, 'PaymentId', { override: true });
});

module.exports = schema;
module.exports = new CaseInsensitiveSchema(schema);
13 changes: 4 additions & 9 deletions lib/domain/InitialOptions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const Joi = require('@hapi/joi');
const { CaseInsensitiveSchema } = require('../schema');

const constraints = require('./_constraints');

/**
Expand Down Expand Up @@ -26,13 +28,6 @@ const schema = Joi.object({
.default('HUF'),
ValidateModels: Joi.boolean().optional()
.default(true)
})
.rename(/^POSKey$/i, 'POSKey', { override: true })
.rename(/^Environment$/i, 'Environment', { override: true })
.rename(/^FundingSources$/i, 'FundingSources', { override: true })
.rename(/^GuestCheckOut$/i, 'GuestCheckOut', { override: true })
.rename(/^Locale$/i, 'Locale', { override: true })
.rename(/^Currency$/i, 'Currency', { override: true })
.rename(/^ValidateModels$/i, 'ValidateModels', { override: true });
});

module.exports = schema;
module.exports = new CaseInsensitiveSchema(schema);
9 changes: 4 additions & 5 deletions lib/domain/PaymentRefund.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const Joi = require('@hapi/joi');
const { CaseInsensitiveSchema } = require('../schema');

const TransactionToRefund = require('./common/TransactionToRefund');

/**
Expand All @@ -13,9 +15,6 @@ const schema = Joi.object({
.guid(),
TransactionsToRefund: Joi.array().required()
.items(TransactionToRefund)
})
.rename(/^POSKey$/i, 'POSKey', { override: true })
.rename(/^PaymentId$/i, 'PaymentId', { override: true })
.rename(/^TransactionsToRefund$/i, 'TransactionsToRefund', { override: true });
});

module.exports = schema;
module.exports = new CaseInsensitiveSchema(schema);
12 changes: 4 additions & 8 deletions lib/domain/SendTransfer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const Joi = require('@hapi/joi');
const { CaseInsensitiveSchema } = require('../schema');

const constraints = require('./_constraints');

/**
Expand All @@ -18,12 +20,6 @@ const schema = Joi.object({
.email(),
Comment: Joi.string().optional()
.max(1000)
})
.rename(/^UserName$/i, 'UserName', { override: true })
.rename(/^Password$/i, 'Password', { override: true })
.rename(/^Currency$/i, 'Currency', { override: true })
.rename(/^Amount$/i, 'Amount', { override: true })
.rename(/^Recipient$/i, 'Recipient', { override: true })
.rename(/^Comment$/i, 'Comment', { override: true });
});

module.exports = schema;
module.exports = new CaseInsensitiveSchema(schema);
24 changes: 4 additions & 20 deletions lib/domain/StartPayment.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const Joi = require('@hapi/joi');
const { CaseInsensitiveSchema } = require('../schema');

const TimeSpan = require('./common/TimeSpan');
const ShippingAddress = require('./common/ShippingAddress');
const PaymentTransaction = require('./common/PaymentTransaction');
Expand Down Expand Up @@ -50,24 +52,6 @@ const schema = Joi.object({
PayerPhoneNumber: Joi.string().optional()
.max(30)
.regex(/^[0-9]{1,}$/, 'mobile')
})
.rename(/^POSKey$/i, 'POSKey', { override: true })
.rename(/^PaymentType$/i, 'PaymentType', { override: true })
.rename(/^ReservationPeriod$/i, 'ReservationPeriod', { override: true })
.rename(/^PaymentWindow$/i, 'PaymentWindow', { override: true })
.rename(/^GuestCheckOut$/i, 'GuestCheckOut', { override: true })
.rename(/^InitiateRecurrence$/i, 'InitiateRecurrence', { override: true })
.rename(/^RecurrenceId$/i, 'RecurrenceId', { override: true })
.rename(/^FundingSources$/i, 'FundingSources', { override: true })
.rename(/^PaymentRequestId$/i, 'PaymentRequestId', { override: true })
.rename(/^PayerHint$/i, 'PayerHint', { override: true })
.rename(/^RedirectUrl$/i, 'RedirectUrl', { override: true })
.rename(/^CallbackUrl$/i, 'CallbackUrl', { override: true })
.rename(/^Transactions$/i, 'Transactions', { override: true })
.rename(/^OrderNumber$/i, 'OrderNumber', { override: true })
.rename(/^ShippingAddress$/i, 'ShippingAddress', { override: true })
.rename(/^Locale$/i, 'Locale', { override: true })
.rename(/^Currency$/i, 'Currency', { override: true })
.rename(/^PayerPhoneNumber$/i, 'PayerPhoneNumber', { override: true });
});

module.exports = schema;
module.exports = new CaseInsensitiveSchema(schema);
12 changes: 3 additions & 9 deletions lib/domain/common/BankAccount.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Joi = require('@hapi/joi');
const { CaseInsensitiveSchema } = require('../../schema');

/**
* This structure represents a bank account that is used as
Expand All @@ -22,13 +23,6 @@ const schema = Joi.object({
SwiftCode: Joi.string().optional()
.min(8)
.max(11)
})
.rename(/^Country$/i, 'Country', { override: true })
.rename(/^Format$/i, 'Format', { override: true })
.rename(/^AccountNumber$/i, 'AccountNumber', { override: true })
.rename(/^Address$/i, 'Address', { override: true })
.rename(/^BankName$/i, 'BankName', { override: true })
.rename(/^BankAddress$/i, 'BankAddress', { override: true })
.rename(/^SwiftCode$/i, 'SwiftCode', { override: true });
});

module.exports = schema;
module.exports = new CaseInsensitiveSchema(schema);
13 changes: 3 additions & 10 deletions lib/domain/common/Item.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Joi = require('@hapi/joi');
const { CaseInsensitiveSchema } = require('../../schema');

/**
* This structure represents an included in a payment transaction. An item can describe a product or service.
Expand All @@ -19,14 +20,6 @@ const schema = Joi.object({
ItemTotal: Joi.number().required(),
SKU: Joi.string().optional()
.max(100)
})
.rename(/^Name$/i, 'Name', { override: true })
.rename(/^Description$/i, 'Description', { override: true })
.rename(/^ImageUrl$/i, 'ImageUrl', { override: true })
.rename(/^Quantity$/i, 'Quantity', { override: true })
.rename(/^Unit$/i, 'Unit', { override: true })
.rename(/^UnitPrice$/i, 'UnitPrice', { override: true })
.rename(/^ItemTotal$/i, 'ItemTotal', { override: true })
.rename(/^SKU$/i, 'SKU', { override: true });
});

module.exports = schema;
module.exports = new CaseInsensitiveSchema(schema);
9 changes: 3 additions & 6 deletions lib/domain/common/PayeeTransaction.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Joi = require('@hapi/joi');
const { CaseInsensitiveSchema } = require('../../schema');

/**
* This structure represents payee transaction included in a payment transaction.
Expand All @@ -16,10 +17,6 @@ const schema = Joi.object({
.greater(0),
Comment: Joi.string().optional()
.max(640)
})
.rename(/^POSTransactionId$/i, 'POSTransactionId', { override: true })
.rename(/^Payee$/i, 'Payee', { override: true })
.rename(/^Total$/i, 'Total', { override: true })
.rename(/^Comment$/i, 'Comment', { override: true });
});

module.exports = schema;
module.exports = new CaseInsensitiveSchema(schema);
14 changes: 5 additions & 9 deletions lib/domain/common/PaymentTransaction.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const Joi = require('@hapi/joi');
const { CaseInsensitiveSchema } = require('../../schema');

const PayeeTransaction = require('./PayeeTransaction');
const Item = require('./Item');

Expand All @@ -17,13 +19,7 @@ const schema = Joi.object({
PayeeTransactions: Joi.array().optional()
.items(PayeeTransaction),
Items: Joi.array().optional()
.items(Item),
})
.rename(/^POSTransactionId$/i, 'POSTransactionId', { override: true })
.rename(/^Payee$/i, 'Payee', { override: true })
.rename(/^Total$/i, 'Total', { override: true })
.rename(/^Comment$/i, 'Comment', { override: true })
.rename(/^PayeeTransactions$/i, 'PayeeTransactions', { override: true })
.rename(/^Items$/i, 'Items', { override: true });
.items(Item)
});

module.exports = schema;
module.exports = new CaseInsensitiveSchema(schema);
14 changes: 3 additions & 11 deletions lib/domain/common/ShippingAddress.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Joi = require('@hapi/joi');
const { CaseInsensitiveSchema } = require('../../schema');

/**
* This structure represents a shipping address related to a payment.
Expand All @@ -17,15 +18,6 @@ const schema = Joi.object({
Street2: Joi.string().optional(),
FullName: Joi.string().optional(),
Phone: Joi.string().optional()
})
.rename(/^DeliveryMethod$/i, 'DeliveryMethod', { override: true })
.rename(/^Country$/i, 'Country', { override: true })
.rename(/^City$/i, 'City', { override: true })
.rename(/^Region$/i, 'Region', { override: true })
.rename(/^Zip$/i, 'Zip', { override: true })
.rename(/^Street$/i, 'Street', { override: true })
.rename(/^Street2$/i, 'Street2', { override: true })
.rename(/^FullName$/i, 'FullName', { override: true })
.rename(/^Phone$/i, 'Phone', { override: true });
});

module.exports = schema;
module.exports = new CaseInsensitiveSchema(schema);
3 changes: 2 additions & 1 deletion lib/domain/common/TimeSpan.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Joi = require('@hapi/joi');
const { CaseInsensitiveSchema } = require('../../schema');

/**
* A time range value represented in a string format of the ISO-8601 standard,
Expand All @@ -8,4 +9,4 @@ const Joi = require('@hapi/joi');
*/
const schema = Joi.string().regex(/^[0-9]{1,}(\.|:)(0[0-9]|1[0-9]|2[0-3]):[0-5]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$/, 'timespan');

module.exports = schema;
module.exports = new CaseInsensitiveSchema(schema);
11 changes: 4 additions & 7 deletions lib/domain/common/TransactionToFinish.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const Joi = require('@hapi/joi');
const { CaseInsensitiveSchema } = require('../../schema');

const PayeeTransaction = require('./PayeeTransaction');
const Item = require('./Item');

Expand All @@ -17,11 +19,6 @@ const schema = Joi.object({
.items(PayeeTransaction),
Items: Joi.array().optional()
.items(Item)
})
.rename(/^TransacionId$/i, 'TransacionId', { override: true })
.rename(/^Total$/i, 'Total', { override: true })
.rename(/^Comment$/i, 'Comment', { override: true })
.rename(/^PayeeTransactions$/i, 'PayeeTransactions', { override: true })
.rename(/^Items$/i, 'Items', { override: true });
});

module.exports = schema;
module.exports = new CaseInsensitiveSchema(schema);
9 changes: 3 additions & 6 deletions lib/domain/common/TransactionToRefund.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Joi = require('@hapi/joi');
const { CaseInsensitiveSchema } = require('../../schema');

/**
* This structure represents a payment transaction that is to be refunded completely or partially.
Expand All @@ -13,10 +14,6 @@ const schema = Joi.object({
AmountToRefund: Joi.number().required()
.greater(0),
Comment: Joi.string().optional()
})
.rename(/^TransactionId$/i, 'TransactionId', { override: true })
.rename(/^POSTransactionId$/i, 'POSTransactionId', { override: true })
.rename(/^AmountToRefund$/i, 'AmountToRefund', { override: true })
.rename(/^Comment$/i, 'Comment', { override: true });
});

module.exports = schema;
module.exports = new CaseInsensitiveSchema(schema);
Loading

0 comments on commit 3ed8738

Please sign in to comment.