Skip to content

Commit

Permalink
Added validation context to handle patch
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineludeau committed Nov 16, 2023
1 parent 1058866 commit 4f001b0
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 88 deletions.
47 changes: 27 additions & 20 deletions lib/api/address/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,36 @@ const metaSchema = object({

export const banAddressSchema = object({
id: banID.required(),
mainCommonToponymID: banID.required(),
mainCommonToponymID: banID.when('$isPatch', {
is: true,
otherwise: schema => schema.required(),
}),
secondaryCommonToponymIDs: array().of(banID),
districtID: banID.required(),
number: number().positive().integer().required(),
districtID: banID.when('$isPatch', {
is: true,
otherwise: schema => schema.required(),
}),
number: number().positive().integer().when('$isPatch', {
is: true,
otherwise: schema => schema.required(),
}),
suffix: string().trim(),
labels: array().of(labelSchema),
labels: array().of(labelSchema).when('$isPatch', {
is: true,
// eslint-disable-next-line unicorn/no-thenable
then: schema => schema.default(null).nullable(),
}),
certified: boolean(),
positions: array().of(positionSchema).required(),
updateDate: date().required(),
meta: metaSchema
}).noUnknown()

export const banAddressSchemaForPatch = object({
id: banID.required(),
mainCommonToponymID: banID,
secondaryCommonToponymIDs: array().of(banID),
districtID: banID,
number: number().positive().integer(),
suffix: string().trim(),
labels: array().of(labelSchema).default(null).nullable(),
certified: boolean().default(false),
positions: array().of(positionSchema).default(null).nullable(),
updateDate: date(),
positions: array().of(positionSchema).when('$isPatch', {
is: true,
// eslint-disable-next-line unicorn/no-thenable
then: schema => schema.default(null).nullable(),
otherwise: schema => schema.required(),
}),
updateDate: date().when('$isPatch', {
is: true,
otherwise: schema => schema.required(),
}),
meta: metaSchema
}).noUnknown()

Expand Down
22 changes: 2 additions & 20 deletions lib/api/address/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {checkDataFormat, dataValidationReportFrom, checkIdsIsUniq, checkIdsIsVacant, checkIdsIsAvailable, checkDataShema, checkIdsShema, checkIfCommonToponymsExist, checkIfDistrictsExist} from '../helper.js'
import {banID} from '../schema.js'
import {getAddresses, getAllAddressIDsFromDistrict, getAllAddressIDsOutsideDistrict} from './models.js'
import {banAddressSchema, banAddressSchemaForPatch} from './schema.js'
import {banAddressSchema} from './schema.js'

const getExistingAddressIDs = async addressIDs => {
const existingAddresses = await getAddresses(addressIDs)
Expand Down Expand Up @@ -50,32 +50,14 @@ export const checkAddressesRequest = async (addresses, actionType) => {
switch (actionType) {
case 'insert':
case 'update':
report = checkDataFormat(
`The request require an Array of address but receive ${typeof addresses}`,
'No address send to job',
addresses
)
|| await checkAddressesIDsRequest(addresses.map(address => address.id), actionType, false)
|| await checkDataShema('Invalid format', addresses, banAddressSchema)
|| await checkIfCommonToponymsExist(addresses.reduce((acc, {mainCommonToponymID, secondaryCommonToponymIDs}) => {
const ids = [mainCommonToponymID]
if (secondaryCommonToponymIDs) {
ids.push(...secondaryCommonToponymIDs)
}

return [...acc, ...ids]
}, []))
|| await checkIfDistrictsExist(addresses.map(({districtID}) => districtID))
|| dataValidationReportFrom(true)
break
case 'patch':
report = checkDataFormat(
`The request require an Array of address but receive ${typeof addresses}`,
'No address send to job',
addresses
)
|| await checkAddressesIDsRequest(addresses.map(address => address.id), actionType, false)
|| await checkDataShema('Invalid format', addresses, banAddressSchemaForPatch)
|| await checkDataShema('Invalid format', addresses, banAddressSchema, {isPatch: actionType === 'patch'})
|| await checkIfCommonToponymsExist(addresses.reduce((acc, {mainCommonToponymID, secondaryCommonToponymIDs}) => {
const ids = mainCommonToponymID ? [mainCommonToponymID] : []
if (secondaryCommonToponymIDs) {
Expand Down
26 changes: 14 additions & 12 deletions lib/api/common-toponym/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ const metaSchema = object({

export const banCommonToponymSchema = object({
id: banID.required(),
districtID: banID.required(),
labels: array().of(labelSchema).required(),
districtID: banID.when('$isPatch', {
is: true,
otherwise: schema => schema.required(),
}),
labels: array().of(labelSchema).when('$isPatch', {
is: true,
// eslint-disable-next-line unicorn/no-thenable
then: schema => schema.default(null).nullable(),
otherwise: schema => schema.required(),
}),
geometry: geometrySchema.default(null).nullable(),
updateDate: date().required(),
meta: metaSchema
}).noUnknown()

export const banCommonToponymSchemaForPatch = object({
id: banID.required(),
districtID: banID,
labels: array().of(labelSchema).default(null).nullable(),
geometry: geometrySchema.default(null).nullable(),
updateDate: date(),
updateDate: date().when('$isPatch', {
is: true,
otherwise: schema => schema.required(),
}),
meta: metaSchema
}).noUnknown()

Expand Down
14 changes: 2 additions & 12 deletions lib/api/common-toponym/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {checkDataFormat, dataValidationReportFrom, checkIdsIsUniq, checkIdsIsVacant, checkIdsIsAvailable, checkDataShema, checkIdsShema, checkIfDistrictsExist} from '../helper.js'
import {banID} from '../schema.js'
import {getCommonToponyms, getAllCommonToponymIDsFromDistrict, getAllCommonToponymIDsOutsideDistrict} from './models.js'
import {banCommonToponymSchema, banCommonToponymSchemaForPatch} from './schema.js'
import {banCommonToponymSchema} from './schema.js'

const getExistingCommonToponymIDs = async commonToponymIDs => {
const existingCommonToponyms = await getCommonToponyms(commonToponymIDs)
Expand Down Expand Up @@ -50,24 +50,14 @@ export const checkCommonToponymsRequest = async (commonToponyms, actionType) =>
switch (actionType) {
case 'insert':
case 'update':
report = checkDataFormat(
`The request require an Array of common toponym but receive ${typeof commonToponyms}`,
'No common toponym send to job',
commonToponyms
)
|| await checkCommonToponymsIDsRequest(commonToponyms.map(commonToponym => commonToponym.id), actionType, false)
|| await checkDataShema('Invalid common toponym format', commonToponyms, banCommonToponymSchema)
|| await checkIfDistrictsExist(commonToponyms.map(({districtID}) => districtID))
|| dataValidationReportFrom(true)
break
case 'patch':
report = checkDataFormat(
`The request require an Array of common toponym but receive ${typeof commonToponyms}`,
'No common toponym send to job',
commonToponyms
)
|| await checkCommonToponymsIDsRequest(commonToponyms.map(commonToponym => commonToponym.id), actionType, false)
|| await checkDataShema('Invalid common toponym format', commonToponyms, banCommonToponymSchemaForPatch)
|| await checkDataShema('Invalid common toponym format', commonToponyms, banCommonToponymSchema, {isPatch: actionType === 'patch'})
|| await checkIfDistrictsExist(commonToponyms.reduce((acc, {districtID}) => {
if (districtID) {
return [...acc, districtID]
Expand Down
39 changes: 25 additions & 14 deletions lib/api/district/schema.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
import {object, array, date, bool} from 'yup'
import {banID, labelSchema, inseeSchema, balSchema} from '../schema.js'
import {object, string, array, date, bool} from 'yup'
import {banID, labelSchema, balSchema} from '../schema.js'

const configSchema = object({
useBanId: bool()
}).noUnknown()

const inseeSchema = object({
cog: string().trim().length(5).when('$isPatch', {
is: true,
otherwise: schema => schema.required(),
}),
})

const metaSchema = object({
insee: inseeSchema,
bal: balSchema,
}).noUnknown()

export const banDistrictSchema = object({
id: banID.required(),
labels: array().of(labelSchema).required(),
updateDate: date().required(),
config: configSchema,
meta: metaSchema
}).noUnknown()

export const banDistrictSchemaForPatch = object({
id: banID.required(),
labels: array().of(labelSchema).default(null).nullable(),
updateDate: date(),
labels: array().of(labelSchema).when('$isPatch', {
is: true,
// eslint-disable-next-line unicorn/no-thenable
then: schema => schema.default(null).nullable(),
otherwise: schema => schema.required(),
}),
updateDate: date().when('$isPatch', {
is: true,
otherwise: schema => schema.required(),
}),
config: configSchema,
meta: metaSchema.default(null).nullable(),
}).noUnknown()
meta: metaSchema.when('$isPatch', {
is: true,
// eslint-disable-next-line unicorn/no-thenable
then: schema => schema.default(null).nullable(),
}),
})

export const districtDefaultOptionalValues = {
config: undefined,
Expand Down
4 changes: 2 additions & 2 deletions lib/api/district/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {checkDataFormat, dataValidationReportFrom, checkIdsIsUniq, checkIdsIsVacant, checkIdsIsAvailable, checkDataShema, checkIdsShema} from '../helper.js'
import {banID} from '../schema.js'
import {getDistricts} from './models.js'
import {banDistrictSchema, banDistrictSchemaForPatch} from './schema.js'
import {banDistrictSchema} from './schema.js'

const getExistingDistrictIDs = async districtIDs => {
const existingDistricts = await getDistricts(districtIDs)
Expand Down Expand Up @@ -57,7 +57,7 @@ export const checkDistrictsRequest = async (districts, actionType) => {
districts
)
|| await checkDistrictsIDsRequest(districts.map(district => district.id), actionType, false)
|| await checkDataShema('Invalid format', districts, actionType === 'patch' ? banDistrictSchemaForPatch : banDistrictSchema)
|| await checkDataShema('Invalid format', districts, banDistrictSchema, {isPatch: actionType === 'patch'})
|| dataValidationReportFrom(true)
break
default:
Expand Down
8 changes: 4 additions & 4 deletions lib/api/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,18 @@ export const checkIdsShema = async (err = 'Invalid IDs format', ids, schema) =>
}
}

const dataSchemaValidation = async (data, schema) => {
const dataSchemaValidation = async (data, schema, context) => {
try {
await schema.validate(data, {strict: true, abortEarly: false})
await schema.validate(data, {strict: true, abortEarly: false, context})
} catch (error) {
return dataValidationReportFrom(false, `Invalid data format (id: ${data.id})`, error.errors)
}

return dataValidationReportFrom(true)
}

export const checkDataShema = async (err = 'Invalid data format', dataArr, schema) => {
const dataArrValidationPromises = dataArr.map(data => dataSchemaValidation(data, schema))
export const checkDataShema = async (err = 'Invalid data format', dataArr, schema, context) => {
const dataArrValidationPromises = dataArr.map(data => dataSchemaValidation(data, schema, context))
const dataArrValidation = await Promise.all(dataArrValidationPromises)
const invalidDataArr = dataArrValidation.filter(({isValid}) => !isValid)
if (invalidDataArr.length > 0) {
Expand Down
4 changes: 0 additions & 4 deletions lib/api/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ export const geometrySchema = object({
coordinates: array().length(2).of(number()).required(),
}).noUnknown()

export const inseeSchema = object({
cog: string().trim().length(5).required()
}).noUnknown()

export const cadastreSchema = object({
ids: array().of(string().trim())
}).noUnknown()
Expand Down

0 comments on commit 4f001b0

Please sign in to comment.