Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
},
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@defra/forms-model": "^3.0.663",
"@defra/forms-model": "^3.0.667",
"@defra/hapi-tracing": "^1.29.0",
"@defra/interactive-map": "^0.0.22-alpha",
"@elastic/ecs-pino-format": "^1.5.0",
Expand Down
305 changes: 302 additions & 3 deletions src/server/plugins/engine/components/GeospatialField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ describe('GeospatialField', () => {
value: getFormData([]),
errors: [
expect.objectContaining({
text: 'Example geospatial field must contain at least 1 items'
text: 'Define at least 1 features'
Comment thread
davidjamesstone marked this conversation as resolved.
})
]
}
Expand Down Expand Up @@ -291,6 +291,171 @@ describe('GeospatialField', () => {
}
]
},
{
description: 'Required with min constraints',
component: {
title: 'Example geospatial field',
name: 'myComponent',
type: ComponentType.GeospatialField,
options: {
required: true
},
schema: {
min: 2
}
} satisfies GeospatialFieldComponent,
assertions: [
{
input: getFormData([]),
output: {
value: getFormData([]),
errors: [
expect.objectContaining({
text: 'Define at least 2 features'
})
]
}
},
{
input: getFormData(),
output: {
value: getFormData(),
errors: [
expect.objectContaining({
text: 'Select example geospatial field'
})
]
}
},
{
input: getFormData(validSingleState),
output: {
value: getFormData(validSingleState),
errors: [
expect.objectContaining({
text: 'Define at least 2 features'
})
]
}
},
{
input: getFormData(validState),
output: {
value: getFormData(validState)
}
}
]
},
{
description: 'Required with max constraints',
component: {
title: 'Example geospatial field',
name: 'myComponent',
type: ComponentType.GeospatialField,
options: {
required: true
},
schema: {
max: 1
}
} satisfies GeospatialFieldComponent,
assertions: [
{
input: getFormData([]),
output: {
value: getFormData([]),
errors: [
expect.objectContaining({
text: 'Define at least 1 features'
Comment thread
davidjamesstone marked this conversation as resolved.
})
]
}
},
{
input: getFormData(),
output: {
value: getFormData(),
errors: [
expect.objectContaining({
text: 'Select example geospatial field'
})
]
}
},
{
input: getFormData(validSingleState),
output: {
value: getFormData(validSingleState)
}
},
{
input: getFormData(validState),
output: {
value: getFormData(validState),
errors: [
expect.objectContaining({
text: 'Only 1 features can be defined'
Comment thread
davidjamesstone marked this conversation as resolved.
})
]
}
}
]
},
{
description: 'Required with exact length constraints',
component: {
title: 'Example geospatial field',
name: 'myComponent',
type: ComponentType.GeospatialField,
options: {
required: true
},
schema: {
length: 1
}
} satisfies GeospatialFieldComponent,
assertions: [
{
input: getFormData([]),
output: {
value: getFormData([]),
errors: [
expect.objectContaining({
text: 'Define exactly 1 features'
Comment thread
davidjamesstone marked this conversation as resolved.
})
]
}
},
{
input: getFormData(),
output: {
value: getFormData(),
errors: [
expect.objectContaining({
text: 'Select example geospatial field'
})
]
}
},
{
input: getFormData(validSingleState),
output: {
value: getFormData(validSingleState)
}
},
{
input: getFormData(validState),
output: {
value: getFormData(validState),
errors: [
expect.objectContaining({
text: 'Define exactly 1 features'
Comment thread
davidjamesstone marked this conversation as resolved.
})
]
}
}
]
},
{
description: 'Optional',
component: {
Expand All @@ -307,14 +472,148 @@ describe('GeospatialField', () => {
output: {
value: getFormData([])
}
}
]
},
{
description: 'Optional with min constraints',
component: {
title: 'Example geospatial field',
name: 'myComponent',
type: ComponentType.GeospatialField,
options: {
required: false
},
schema: {
min: 2
}
} satisfies GeospatialFieldComponent,
assertions: [
{
input: getFormData([]),
output: {
value: getFormData([]),
errors: [
expect.objectContaining({
text: 'Define at least 2 features'
})
]
}
},
{
input: getFormData(),
output: {
value: getFormData(),
value: getFormData()
}
},
{
input: getFormData(validSingleState),
output: {
value: getFormData(validSingleState),
errors: [
expect.objectContaining({
text: 'Select example geospatial field'
text: 'Define at least 2 features'
})
]
}
},
{
input: getFormData(validState),
output: {
value: getFormData(validState)
}
}
]
},
{
description: 'Optional with max constraints',
component: {
title: 'Example geospatial field',
name: 'myComponent',
type: ComponentType.GeospatialField,
options: {
required: false
},
schema: {
max: 1
}
} satisfies GeospatialFieldComponent,
assertions: [
{
input: getFormData([]),
output: {
value: getFormData([])
}
},
{
input: getFormData(),
output: {
value: getFormData()
}
},
{
input: getFormData(validSingleState),
output: {
value: getFormData(validSingleState)
}
},
{
input: getFormData(validState),
output: {
value: getFormData(validState),
errors: [
expect.objectContaining({
text: 'Only 1 features can be defined'
Comment thread
davidjamesstone marked this conversation as resolved.
})
]
}
}
]
},
{
description: 'Optional with exact length constraints',
component: {
title: 'Example geospatial field',
name: 'myComponent',
type: ComponentType.GeospatialField,
options: {
required: false
},
schema: {
length: 1
}
} satisfies GeospatialFieldComponent,
assertions: [
{
input: getFormData([]),
output: {
value: getFormData([]),
errors: [
expect.objectContaining({
text: 'Define exactly 1 features'
Comment thread
davidjamesstone marked this conversation as resolved.
})
]
}
},
{
input: getFormData(),
output: {
value: getFormData()
}
},
{
input: getFormData(validSingleState),
output: {
value: getFormData(validSingleState)
}
},
{
input: getFormData(validState),
output: {
value: getFormData(validState),
errors: [
expect.objectContaining({
text: 'Define exactly 1 features'
})
]
}
Expand Down
17 changes: 10 additions & 7 deletions src/server/plugins/engine/components/GeospatialField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,21 @@ export class GeospatialField extends FormComponent {

const { options } = def

let formSchema = getGeospatialSchema(options.countries?.at(0))
const formSchema = getGeospatialSchema(def)
.label(this.label)
.required()
.messages({
'array.min': messageTemplate.featuresMin as string,
'array.max': messageTemplate.featuresMax as string,
'array.length': messageTemplate.featuresLength as string
})

formSchema = formSchema.max(50)
this.formSchema = formSchema
this.stateSchema = formSchema.default(null)

if (options.required !== false) {
formSchema = formSchema.min(1)
if (options.required === false) {
this.stateSchema = this.stateSchema.allow(null)
}

this.formSchema = formSchema
this.stateSchema = formSchema.default(null)
this.options = options
}

Expand Down
Loading
Loading