Skip to content
Draft
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
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'
})
]
}
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'
})
]
}
},
{
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'
})
]
}
}
]
},
{
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'
})
]
}
},
{
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'
})
]
}
}
]
},
{
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'
})
]
}
}
]
},
{
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'
})
]
}
},
{
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