Skip to content

Commit

Permalink
fix: allow child table updates in schema
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Jan 27, 2022
1 parent 534e01f commit d0b5dc4
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 34 deletions.
9 changes: 6 additions & 3 deletions src/validations/co-benefits.validations.js
Original file line number Diff line number Diff line change
@@ -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,
});
9 changes: 6 additions & 3 deletions src/validations/issuances.validation.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import Joi from 'joi';

export const newIssuanceScheme = Joi.object({
const baseSchema = {
// orgUid - derived upon creation
// warehouseProjectId - derived upon creation
startDate: Joi.date().required(),
endDate: Joi.date().required(),
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,
});
11 changes: 8 additions & 3 deletions src/validations/labels.validations.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import Joi from 'joi';

export const newLabelScheme = Joi.object({
const baseSchema = {
label: Joi.string().required(),
creditingPeriodStartDate: Joi.date().required(),
creditingPeriodEndDate: Joi.date().required(),
validityStartDate: Joi.date().required(),
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,
});
9 changes: 6 additions & 3 deletions src/validations/locations.validations.js
Original file line number Diff line number Diff line change
@@ -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,
});
53 changes: 41 additions & 12 deletions src/validations/projects.validations.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions src/validations/ratings.validations.js
Original file line number Diff line number Diff line change
@@ -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,
});
9 changes: 6 additions & 3 deletions src/validations/relatedProjects.validations.js
Original file line number Diff line number Diff line change
@@ -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,
});
8 changes: 4 additions & 4 deletions src/validations/units.validations.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(),
};

Expand Down

0 comments on commit d0b5dc4

Please sign in to comment.