Skip to content

Commit

Permalink
Break schema down
Browse files Browse the repository at this point in the history
TBH 50/50 on whether this improves things or not. We hope it makes the schemas for the individual properties easy to see and understand so we're going for it.

There is an argument though that seeing the whole schema in one place makes it easier to see what Joi is doing. ¯\_(ツ)_/¯
  • Loading branch information
Cruikshanks committed Aug 29, 2023
1 parent 58b9165 commit 7f121f3
Showing 1 changed file with 45 additions and 33 deletions.
78 changes: 45 additions & 33 deletions app/validators/change-address.validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,56 @@ const StaticLookupsLib = require('../lib/static-lookups.lib.js')
*/
function go (data) {
const schema = Joi.object({
agentCompany: Joi.object({
id: Joi.string().guid().optional(),
type: Joi.string().valid(...StaticLookupsLib.companyTypes).optional(),
organisationType: Joi.string().valid(...StaticLookupsLib.organisationTypes).optional(),
name: Joi.string().optional(),
companyNumber: Joi.string().optional()
}).optional(),
contact: Joi.object({
id: Joi.string().guid().optional(),
type: Joi.string().valid(...StaticLookupsLib.contactTypes).optional(),
salutation: Joi.string().optional(),
firstName: Joi.string().optional(),
initials: Joi.string().optional(),
middleInitials: Joi.string().optional(),
lastName: Joi.string().optional(),
suffix: Joi.string().optional(),
department: Joi.string().optional(),
source: Joi.string().valid(...StaticLookupsLib.sources).optional(),
isTest: Joi.boolean().default(false).optional()
}).optional(),
address: Joi.object({
id: Joi.string().guid().optional(),
addressLine1: Joi.string().optional(),
addressLine2: Joi.string().optional(),
addressLine3: Joi.string().optional(),
addressLine4: Joi.string().optional(),
town: Joi.string().optional(),
county: Joi.string().optional(),
country: Joi.string().optional(),
postcode: Joi.string().optional(),
uprn: Joi.number().optional(),
source: Joi.string().optional()
}).required()
address: _addressSchema(),
agentCompany: _agentCompanySchema(),
contact: _contactSchema()
})

return schema.validate(data)
}

function _addressSchema () {
return Joi.object({
id: Joi.string().guid().optional(),
addressLine1: Joi.string().optional(),
addressLine2: Joi.string().optional(),
addressLine3: Joi.string().optional(),
addressLine4: Joi.string().optional(),
town: Joi.string().optional(),
county: Joi.string().optional(),
country: Joi.string().optional(),
postcode: Joi.string().optional(),
uprn: Joi.number().optional(),
source: Joi.string().optional()
}).required()
}

function _agentCompanySchema () {
return Joi.object({
id: Joi.string().guid().optional(),
type: Joi.string().valid(...StaticLookupsLib.companyTypes).optional(),
organisationType: Joi.string().valid(...StaticLookupsLib.organisationTypes).optional(),
name: Joi.string().optional(),
companyNumber: Joi.string().optional()
}).optional()
}

function _contactSchema () {
return Joi.object({
id: Joi.string().guid().optional(),
type: Joi.string().valid(...StaticLookupsLib.contactTypes).optional(),
salutation: Joi.string().optional(),
firstName: Joi.string().optional(),
initials: Joi.string().optional(),
middleInitials: Joi.string().optional(),
lastName: Joi.string().optional(),
suffix: Joi.string().optional(),
department: Joi.string().optional(),
source: Joi.string().valid(...StaticLookupsLib.sources).optional(),
isTest: Joi.boolean().default(false).optional()
}).optional()
}

module.exports = {
go
}

0 comments on commit 7f121f3

Please sign in to comment.