Skip to content

Commit

Permalink
fix: remove extraneous joi alternative schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Jan 27, 2022
1 parent 5a45a35 commit 12357ce
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 97 deletions.
10 changes: 2 additions & 8 deletions src/validations/co-benefits.validations.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import Joi from 'joi';

const baseSchema = {
export const cobenefitSchema = Joi.object({
id: Joi.string().optional(),
cobenefit: Joi.string().required(),
};

export const newCobenefitSchema = Joi.object({ ...baseSchema });

export const existingCobenefitSchema = Joi.object({
id: Joi.string().required(),
...baseSchema,
});
10 changes: 2 additions & 8 deletions src/validations/issuances.validation.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import Joi from 'joi';

const baseSchema = {
export const issuanceSchema = Joi.object({
// orgUid - derived upon creation
// warehouseProjectId - derived upon creation
id: Joi.string().optional(),
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({
id: Joi.string().required(),
...baseSchema,
});
12 changes: 2 additions & 10 deletions src/validations/labels.validations.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import Joi from 'joi';

const baseSchema = {
export const labelSchema = Joi.object({
id: Joi.string().optional(),
label: Joi.string().required(),
creditingPeriodStartDate: Joi.string().required(),
creditingPeriodEndDate: Joi.string().required(),
validityPeriodStartDate: Joi.string().required(),
validityPeriodEndDate: Joi.string().required(),
unitQuantity: Joi.number().integer().required(),
labelLink: Joi.string().required(),
};

export const newLabelSchema = Joi.object({
...baseSchema,
});

export const existingLabelSchema = Joi.object({
id: Joi.string().required(),
...baseSchema,
});
10 changes: 2 additions & 8 deletions src/validations/locations.validations.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import Joi from 'joi';

const baseSchema = {
export const locationSchema = Joi.object({
id: Joi.string().optional(),
country: Joi.string().required(),
inCountryRegion: Joi.string().required(),
geographicIdentifier: Joi.string().required(),
};

export const newLocationSchema = Joi.object({ ...baseSchema });

export const existingLocationSchema = Joi.object({
id: Joi.string().required(),
...baseSchema,
});
53 changes: 12 additions & 41 deletions src/validations/projects.validations.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import Joi from 'joi';
import {
newCobenefitSchema,
newLocationSchema,
newRatingSchema,
newRelatedProjectSchema,
newLabelSchema,
newIssuanceSchema,
existingCobenefitSchema,
existingLocationSchema,
existingRatingSchema,
existingRelatedProjectSchema,
existingLabelSchema,
existingIssuanceSchema,
cobenefitSchema,
locationSchema,
ratingSchema,
relatedProjectSchema,
labelSchema,
issuanceSchema,
} from '../validations';

export const baseSchema = {
Expand All @@ -37,35 +31,12 @@ export const baseSchema = {
validationDate: Joi.string().optional(),

/* Child Tables */
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(),
labels: Joi.array().items(labelSchema).min(1).optional(),
issuances: Joi.array().items(issuanceSchema).min(1).optional(),
coBenefits: Joi.array().items(cobenefitSchema).min(1).optional(),
relatedProjects: Joi.array().items(relatedProjectSchema).min(1).optional(),
projectLocations: Joi.array().items(locationSchema).min(1).optional(),
projectRatings: Joi.array().items(ratingSchema).min(1).optional(),
};

export const projectsGetQuerySchema = Joi.object()
Expand Down
10 changes: 2 additions & 8 deletions src/validations/ratings.validations.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import Joi from 'joi';

const baseSchema = {
export const ratingSchema = Joi.object({
id: Joi.string().optional(),
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({
id: Joi.string().required(),
...baseSchema,
});
10 changes: 2 additions & 8 deletions src/validations/relatedProjects.validations.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import Joi from 'joi';

const baseSchema = {
export const relatedProjectSchema = Joi.object({
// warehouseProjectId - derived upon creation
id: Joi.string().optional(),
relationshipType: Joi.string().optional(),
registry: Joi.string().optional(),
};

export const newRelatedProjectSchema = Joi.object({ ...baseSchema });

export const existingRelatedProjectSchema = Joi.object({
id: Joi.string().required(),
...baseSchema,
});
10 changes: 4 additions & 6 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 { newIssuanceSchema } from './issuances.validation';
import { newLabelSchema, existingLabelSchema } from './labels.validations';
import { issuanceSchema } from './issuances.validation';
import { labelSchema } from './labels.validations';

const customSerialNumberValidator = (obj, helper) => {
const { serialNumberBlock, serialNumberPattern } = obj;
Expand Down Expand Up @@ -56,10 +56,8 @@ const unitsBaseSchema = {
correspondingAdjustmentStatus: Joi.string()
.valid('Unknown', 'Not Started', 'Pending')
.required(),
issuance: Joi.alternatives().try(newIssuanceSchema).optional(),
labels: Joi.array()
.items(Joi.alternatives().try(newLabelSchema, existingLabelSchema))
.optional(),
issuance: issuanceSchema.optional(),
labels: Joi.array().items(labelSchema).optional(),
};

export const unitsPostSchema = Joi.object({
Expand Down

0 comments on commit 12357ce

Please sign in to comment.