From d0b5dc4336c0217c382506d13f35f9b7fe4a657b Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Thu, 27 Jan 2022 09:34:34 -0500 Subject: [PATCH] fix: allow child table updates in schema --- src/validations/co-benefits.validations.js | 9 ++-- src/validations/issuances.validation.js | 9 ++-- src/validations/labels.validations.js | 11 ++-- src/validations/locations.validations.js | 9 ++-- src/validations/projects.validations.js | 53 ++++++++++++++----- src/validations/ratings.validations.js | 9 ++-- .../relatedProjects.validations.js | 9 ++-- src/validations/units.validations.js | 8 +-- 8 files changed, 83 insertions(+), 34 deletions(-) diff --git a/src/validations/co-benefits.validations.js b/src/validations/co-benefits.validations.js index 2b432de2..68d7d196 100644 --- a/src/validations/co-benefits.validations.js +++ b/src/validations/co-benefits.validations.js @@ -1,9 +1,12 @@ import Joi from 'joi'; -export const newCobenefitScheme = Joi.object({ +const baseSchema = { cobenefit: Joi.string().required(), -}); +}; + +export const newCobenefitSchema = Joi.object({ ...baseSchema }); export const existingCobenefitSchema = Joi.object({ - cobenefitId: Joi.string().required(), + id: Joi.string().required(), + ...baseSchema, }); diff --git a/src/validations/issuances.validation.js b/src/validations/issuances.validation.js index 31019721..e6f9416c 100644 --- a/src/validations/issuances.validation.js +++ b/src/validations/issuances.validation.js @@ -1,6 +1,6 @@ import Joi from 'joi'; -export const newIssuanceScheme = Joi.object({ +const baseSchema = { // orgUid - derived upon creation // warehouseProjectId - derived upon creation startDate: Joi.date().required(), @@ -8,8 +8,11 @@ export const newIssuanceScheme = Joi.object({ verificationApproach: Joi.string().required(), verificationDate: Joi.date().required(), verificationBody: Joi.string().required(), -}); +}; + +export const newIssuanceSchema = Joi.object({ ...baseSchema }); export const existingIssuanceSchema = Joi.object({ - issuanceId: Joi.string().required(), + id: Joi.string().required(), + ...baseSchema, }); diff --git a/src/validations/labels.validations.js b/src/validations/labels.validations.js index 5b127560..915d72b9 100644 --- a/src/validations/labels.validations.js +++ b/src/validations/labels.validations.js @@ -1,6 +1,6 @@ import Joi from 'joi'; -export const newLabelScheme = Joi.object({ +const baseSchema = { label: Joi.string().required(), creditingPeriodStartDate: Joi.date().required(), creditingPeriodEndDate: Joi.date().required(), @@ -8,8 +8,13 @@ export const newLabelScheme = Joi.object({ validityPeriodEndDate: Joi.date().required(), unitQuantity: Joi.number().integer().required(), labelLink: Joi.string().required(), +}; + +export const newLabelSchema = Joi.object({ + ...baseSchema, }); -export const existingLabelScheme = Joi.object({ - labelId: Joi.string().required(), +export const existingLabelSchema = Joi.object({ + id: Joi.string().required(), + ...baseSchema, }); diff --git a/src/validations/locations.validations.js b/src/validations/locations.validations.js index c72836a1..4e33d2a4 100644 --- a/src/validations/locations.validations.js +++ b/src/validations/locations.validations.js @@ -1,11 +1,14 @@ import Joi from 'joi'; -export const newLocationScheme = Joi.object({ +const baseSchema = { country: Joi.string().required(), inCountryRegion: Joi.string().required(), geographicIdentifier: Joi.string().required(), -}); +}; + +export const newLocationSchema = Joi.object({ ...baseSchema }); export const existingLocationSchema = Joi.object({ - projectLocationId: Joi.string().required(), + id: Joi.string().required(), + ...baseSchema, }); diff --git a/src/validations/projects.validations.js b/src/validations/projects.validations.js index fa4c9359..aef8f0ec 100644 --- a/src/validations/projects.validations.js +++ b/src/validations/projects.validations.js @@ -1,11 +1,17 @@ import Joi from 'joi'; import { - newCobenefitScheme, - newLocationScheme, - newRatingScheme, - newRelatedProjectScheme, - newLabelScheme, - newIssuanceScheme, + newCobenefitSchema, + newLocationSchema, + newRatingSchema, + newRelatedProjectSchema, + newLabelSchema, + newIssuanceSchema, + existingCobenefitSchema, + existingLocationSchema, + existingRatingSchema, + existingRelatedProjectSchema, + existingLabelSchema, + existingIssuanceSchema, } from '../validations'; export const baseSchema = { @@ -31,12 +37,35 @@ export const baseSchema = { validationDate: Joi.string().optional(), /* Child Tables */ - labels: Joi.array().items(newLabelScheme).min(1).optional(), - issuances: Joi.array().items(newIssuanceScheme).min(1).optional(), - coBenefits: Joi.array().items(newCobenefitScheme).min(1).optional(), - relatedProjects: Joi.array().items(newRelatedProjectScheme).min(1).optional(), - projectLocations: Joi.array().items(newLocationScheme).min(1).optional(), - projectRatings: Joi.array().items(newRatingScheme).min(1).optional(), + labels: Joi.array() + .items(Joi.alternatives().try(newLabelSchema, existingLabelSchema)) + .min(1) + .optional(), + issuances: Joi.array() + .items(Joi.alternatives().try(newIssuanceSchema, existingIssuanceSchema)) + .min(1) + .optional(), + coBenefits: Joi.array() + .items(Joi.alternatives().try(newCobenefitSchema, existingCobenefitSchema)) + .min(1) + .optional(), + relatedProjects: Joi.array() + .items( + Joi.alternatives().try( + newRelatedProjectSchema, + existingRelatedProjectSchema, + ), + ) + .min(1) + .optional(), + projectLocations: Joi.array() + .items(Joi.alternatives().try(newLocationSchema, existingLocationSchema)) + .min(1) + .optional(), + projectRatings: Joi.array() + .items(Joi.alternatives().try(newRatingSchema, existingRatingSchema)) + .min(1) + .optional(), }; export const projectsGetQuerySchema = Joi.object() diff --git a/src/validations/ratings.validations.js b/src/validations/ratings.validations.js index f3650a6f..6895cbff 100644 --- a/src/validations/ratings.validations.js +++ b/src/validations/ratings.validations.js @@ -1,13 +1,16 @@ import Joi from 'joi'; -export const newRatingScheme = Joi.object({ +const baseSchema = { ratingType: Joi.string().required(), ratingRangeHighest: Joi.number().integer().required(), ratingRangeLowest: Joi.number().integer().required(), rating: Joi.number().integer().required(), ratingLink: Joi.string().optional(), -}); +}; + +export const newRatingSchema = Joi.object({ ...baseSchema }); export const existingRatingSchema = Joi.object({ - ratingId: Joi.string().required(), + id: Joi.string().required(), + ...baseSchema, }); diff --git a/src/validations/relatedProjects.validations.js b/src/validations/relatedProjects.validations.js index d4f14151..279c6c49 100644 --- a/src/validations/relatedProjects.validations.js +++ b/src/validations/relatedProjects.validations.js @@ -1,11 +1,14 @@ import Joi from 'joi'; -export const newRelatedProjectScheme = Joi.object({ +const baseSchema = { // warehouseProjectId - derived upon creation relationshipType: Joi.string().optional(), registry: Joi.string().optional(), -}); +}; + +export const newRelatedProjectSchema = Joi.object({ ...baseSchema }); export const existingRelatedProjectSchema = Joi.object({ - relatedProjectId: Joi.string().required(), + id: Joi.string().required(), + ...baseSchema, }); diff --git a/src/validations/units.validations.js b/src/validations/units.validations.js index 77ddeca4..9933ed1e 100644 --- a/src/validations/units.validations.js +++ b/src/validations/units.validations.js @@ -1,7 +1,7 @@ import Joi from 'joi'; import { transformSerialNumberBlock } from '../utils/helpers'; -import { newIssuanceScheme } from './issuances.validation'; -import { newLabelScheme, existingLabelScheme } from './labels.validations'; +import { newIssuanceSchema } from './issuances.validation'; +import { newLabelSchema, existingLabelSchema } from './labels.validations'; const customSerialNumberValidator = (obj, helper) => { const { serialNumberBlock, serialNumberPattern } = obj; @@ -56,9 +56,9 @@ const unitsBaseSchema = { correspondingAdjustmentStatus: Joi.string() .valid('Unknown', 'Not Started', 'Pending') .required(), - issuance: Joi.alternatives().try(newIssuanceScheme).optional(), + issuance: Joi.alternatives().try(newIssuanceSchema).optional(), labels: Joi.array() - .items(Joi.alternatives().try(newLabelScheme, existingLabelScheme)) + .items(Joi.alternatives().try(newLabelSchema, existingLabelSchema)) .optional(), };