Skip to content

Commit

Permalink
Added strict mode and noUnknown to validation
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineludeau committed Nov 7, 2023
1 parent 250b006 commit c1e8c13
Showing 11 changed files with 38 additions and 38 deletions.
12 changes: 6 additions & 6 deletions lib/api/address/__mocks__/address-data-mock.js
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ export const addressMock = [
coordinates: [1.23, 2.34]
}
}],
updateDate: '2023-04-12'
updateDate: new Date('2023-04-12')
},
{
id: '00000000-0000-4fff-9fff-00000000000b',
@@ -28,7 +28,7 @@ export const addressMock = [
coordinates: [1.24, 2.34]
}
}],
updateDate: '2023-04-12'
updateDate: new Date('2023-04-12')
},
{
id: '00000000-0000-4fff-9fff-00000000000c',
@@ -43,7 +43,7 @@ export const addressMock = [
coordinates: [1.25, 2.34]
}
}],
updateDate: '2023-04-12'
updateDate: new Date('2023-04-12')
}
]

@@ -84,7 +84,7 @@ export const bddAddressMock = [
coordinates: [1.25, 2.34]
}
}],
updateDate: '2023-04-12'
updateDate: new Date('2023-04-12')
},
{
id: '00000000-0000-4fff-9fff-00000000002b',
@@ -99,7 +99,7 @@ export const bddAddressMock = [
coordinates: [1.25, 2.34]
}
}],
updateDate: '2023-04-12'
updateDate: new Date('2023-04-12')
},
{
id: '00000000-0000-4fff-9fff-00000000002c',
@@ -114,6 +114,6 @@ export const bddAddressMock = [
coordinates: [1.25, 2.34]
}
}],
updateDate: '2023-04-12'
updateDate: new Date('2023-04-12')
}
]
8 changes: 4 additions & 4 deletions lib/api/address/schema.js
Original file line number Diff line number Diff line change
@@ -6,12 +6,12 @@ const PositionTypes = ['entrance', 'building', 'staircase identifier', 'unit ide
const positionSchema = object({
type: string().trim().oneOf(PositionTypes).required(),
geometry: geometrySchema.required(),
})
}).noUnknown()

const metaSchema = object({
cadastre: cadastreSchema,
bal: balSchema,
})
}).noUnknown()

export const banAddressSchema = object({
id: banID.required(),
@@ -25,7 +25,7 @@ export const banAddressSchema = object({
positions: array().of(positionSchema).required(),
updateDate: date().required(),
meta: metaSchema
})
}).noUnknown()

export const banAddressSchemaForPatch = object({
id: banID.required(),
@@ -39,7 +39,7 @@ export const banAddressSchemaForPatch = object({
positions: array().of(positionSchema).default(null).nullable(),
updateDate: date(),
meta: metaSchema
})
}).noUnknown()

export const addressDefaultOptionalValues = {
secondaryCommonToponymIDs: [],
2 changes: 1 addition & 1 deletion lib/api/address/utils.spec.js
Original file line number Diff line number Diff line change
@@ -128,7 +128,7 @@ describe('checkAddressesRequest', () => {
})

it('Available addresses on update', async () => {
const addressesValidation = await checkAddressesRequest(bddAddressMock.map(addr => ({...addr, certifie: true})), 'update')
const addressesValidation = await checkAddressesRequest(bddAddressMock.map(addr => ({...addr, certified: true})), 'update')
const testSchema = await addressesValidationSchema.isValid(addressesValidation, {strict: true})
expect(testSchema).toBe(true)
expect(addressesValidation?.isValid).toBe(true)
12 changes: 6 additions & 6 deletions lib/api/common-toponym/__mocks__/common-toponym-data-mock.js
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ export const commonToponymMock = [
type: 'Point',
coordinates: [1.2345, 2.3456]
},
updateDate: '2023-04-24'
updateDate: new Date('2023-04-24')
},
{
id: '00000000-0000-4fff-9fff-00000000000b',
@@ -23,7 +23,7 @@ export const commonToponymMock = [
type: 'Point',
coordinates: [1.2345, 2.3456]
},
updateDate: '2023-04-24'
updateDate: new Date('2023-04-24')
}
]

@@ -45,7 +45,7 @@ export const commonToponymMockForPatch = [
type: 'Point',
coordinates: [1.2346, 2.3456]
},
updateDate: '2023-04-24'
updateDate: new Date('2023-04-24')
}
]

@@ -61,7 +61,7 @@ export const bddCommonToponymMock = [
type: 'Point',
coordinates: [1.2345, 2.3456]
},
updateDate: '2023-04-24'
updateDate: new Date('2023-04-24')
},
{
id: '00000000-0000-4fff-9fff-00000000001b',
@@ -74,7 +74,7 @@ export const bddCommonToponymMock = [
type: 'Point',
coordinates: [1.2345, 2.3456]
},
updateDate: '2023-04-24'
updateDate: new Date('2023-04-24')
},
{
id: '00000000-0000-4fff-9fff-00000000001c',
@@ -87,6 +87,6 @@ export const bddCommonToponymMock = [
type: 'Point',
coordinates: [1.2345, 2.3456]
},
updateDate: '2023-04-24'
updateDate: new Date('2023-04-24')
}
]
6 changes: 3 additions & 3 deletions lib/api/common-toponym/schema.js
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import {banID, geometrySchema, labelSchema, cadastreSchema, balSchema} from '../
const metaSchema = object({
cadastre: cadastreSchema,
bal: balSchema,
})
}).noUnknown()

export const banCommonToponymSchema = object({
id: banID.required(),
@@ -13,7 +13,7 @@ export const banCommonToponymSchema = object({
geometry: geometrySchema.default(null).nullable(),
updateDate: date().required(),
meta: metaSchema
})
}).noUnknown()

export const banCommonToponymSchemaForPatch = object({
id: banID.required(),
@@ -22,7 +22,7 @@ export const banCommonToponymSchemaForPatch = object({
geometry: geometrySchema.default(null).nullable(),
updateDate: date(),
meta: metaSchema
})
}).noUnknown()

export const commonToponymDefaultOptionalValues = {
geometry: undefined,
2 changes: 1 addition & 1 deletion lib/api/common-toponym/utils.spec.js
Original file line number Diff line number Diff line change
@@ -111,7 +111,7 @@ describe('checkCommonToponymsRequest', () => {
})

it('Available commonToponyms on update', async () => {
const commonToponymsValidation = await checkCommonToponymsRequest(bddCommonToponymMock.map(commonToponym => ({...commonToponym, label: [{isoCode: 'fra', value: 'Rue de la mouette'}]})), 'update')
const commonToponymsValidation = await checkCommonToponymsRequest(bddCommonToponymMock.map(commonToponym => ({...commonToponym, labels: [{isoCode: 'fra', value: 'Rue de la mouette'}]})), 'update')
const testSchema = await commonToponymsValidationSchema.isValid(commonToponymsValidation, {strict: true})
expect(testSchema).toBe(true)
expect(commonToponymsValidation?.isValid).toBe(true)
12 changes: 6 additions & 6 deletions lib/api/district/__mocks__/district-data-mock.js
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ export const districtMock = [
isoCode: 'fra',
value: 'Commune A'
}],
updateDate: '2023-06-22',
updateDate: new Date('2023-06-22'),
meta: {
insee: {
cog: '12345'
@@ -18,7 +18,7 @@ export const districtMock = [
isoCode: 'fra',
value: 'Commune B'
}],
updateDate: '2023-06-22',
updateDate: new Date('2023-06-22'),
meta: {
insee: {
cog: '12346'
@@ -37,7 +37,7 @@ export const districtMockForPatch = [
},
{
id: '00000000-0000-4fff-9fff-000000000001',
updateDate: '2023-06-23',
updateDate: new Date('2023-06-23'),
},
{
id: '00000000-0000-4fff-9fff-000000000002',
@@ -56,7 +56,7 @@ export const bddDistrictMock = [
isoCode: 'fra',
value: 'Commune C'
}],
updateDate: '2023-06-22',
updateDate: new Date('2023-06-22'),
meta: {
insee: {
cog: '54321'
@@ -69,7 +69,7 @@ export const bddDistrictMock = [
isoCode: 'fra',
value: 'Commune D'
}],
updateDate: '2023-06-22',
updateDate: new Date('2023-06-22'),
meta: {
insee: {
cog: '54322'
@@ -82,7 +82,7 @@ export const bddDistrictMock = [
isoCode: 'fra',
value: 'Commune E'
}],
updateDate: '2023-06-22',
updateDate: new Date('2023-06-22'),
meta: {
insee: {
cog: '54323'
8 changes: 4 additions & 4 deletions lib/api/district/schema.js
Original file line number Diff line number Diff line change
@@ -3,28 +3,28 @@ import {banID, labelSchema, inseeSchema, balSchema} from '../schema.js'

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

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(),
config: configSchema,
meta: metaSchema.default(null).nullable(),
})
}).noUnknown()

export const districtDefaultOptionalValues = {
config: undefined,
2 changes: 1 addition & 1 deletion lib/api/district/utils.spec.js
Original file line number Diff line number Diff line change
@@ -94,7 +94,7 @@ describe('checkDistrictsRequest', () => {
})

it('Available districts on update', async () => {
const districtsValidation = await checkDistrictsRequest(bddDistrictMock.map(district => ({...district, label: [{isoCode: 'fra', value: 'commune F'}]})), 'update')
const districtsValidation = await checkDistrictsRequest(bddDistrictMock.map(district => ({...district, labels: [{isoCode: 'fra', value: 'commune F'}]})), 'update')
const testSchema = await districtsValidationSchema.isValid(districtsValidation, {strict: true})
expect(testSchema).toBe(true)
expect(districtsValidation?.isValid).toBe(true)
2 changes: 1 addition & 1 deletion lib/api/helper.js
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ export const checkIdsShema = async (err = 'Invalid IDs format', ids, schema) =>

const dataSchemaValidation = async (data, schema) => {
try {
await schema.validate(data, {abortEarly: false, stripUnknown: true})
await schema.validate(data, {strict: true, abortEarly: false})
} catch (error) {
return dataValidationReportFrom(false, `Invalid data format (id: ${data.id})`, error.errors)
}
10 changes: 5 additions & 5 deletions lib/api/schema.js
Original file line number Diff line number Diff line change
@@ -5,26 +5,26 @@ export const banID = string().trim().uuid()
export const labelSchema = object({
isoCode: string().trim().length(3).required(),
value: string().trim().required(),
})
}).noUnknown()

export const geometrySchema = object({
type: string().trim().matches(/^Point$/).required(),
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()

export const balSchema = object({
idRevision: string().trim(),
dateRevision: date(),
codeAncienneCommune: string().trim(),
nomAncienneCommune: string().trim(),
isLieuDit: boolean()
})
}).noUnknown()

0 comments on commit c1e8c13

Please sign in to comment.