Skip to content

Commit ff69c43

Browse files
committed
[service] fix required field validation fails on numeric value 0
1 parent 1b06e14 commit ff69c43

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

service/src/entities/observations/entities.observations.fields.required.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { SimpleFieldValidation } from './entities.observations.fields'
33

44
export const RequiredFieldValidation: SimpleFieldValidation = function RequiredFieldValidation(field, fieldEntry, result) {
55
if (field.required) {
6-
if (!fieldEntry) {
6+
if (fieldEntry === null || fieldEntry === undefined || fieldEntry === '') {
77
return result.failedBecauseTheEntry('is required', FieldConstraintKey.Required)
88
}
99
}

service/test/entities/observations/entities.observations.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,69 @@ describe('observation entities', function() {
710710
expect(invalid.hasErrors).to.be.false
711711
})
712712

713+
it('passes when a required numeric field value is zero', function() {
714+
715+
const form: Form = {
716+
id: 0,
717+
fields: [
718+
{
719+
id: 1,
720+
name: FormFieldType.Numeric,
721+
required: true,
722+
title: 'Field 8',
723+
type: FormFieldType.Numeric,
724+
min: 0,
725+
max: 100,
726+
},
727+
],
728+
archived: false,
729+
color: 'green',
730+
name: 'form0',
731+
userFields: [],
732+
}
733+
mageEventAttrs = {
734+
...mageEventAttrs,
735+
forms: [ form ]
736+
}
737+
const invalidFormEntryUndef: FormEntry = {
738+
id: 'entry0',
739+
formId: form.id,
740+
}
741+
const invalidFormEntryNull: FormEntry = {
742+
...invalidFormEntryUndef,
743+
[FormFieldType.Numeric]: null
744+
}
745+
const validFormEntry: FormEntry = {
746+
id: invalidFormEntryUndef.id,
747+
formId: form.id,
748+
[FormFieldType.Numeric]: 0,
749+
}
750+
const observationAttrs = makeObservationAttrs(mageEventAttrs.id)
751+
const mageEvent = new MageEvent(mageEventAttrs)
752+
observationAttrs.properties.forms = [ invalidFormEntryUndef ]
753+
let invalid = validateObservation(observationAttrs, mageEvent)
754+
755+
expect(invalid.formEntryErrors.length).to.equal(1)
756+
let formEntryError = new Map(invalid.formEntryErrors).get(0)
757+
expect(formEntryError).to.exist
758+
expect(formEntryError?.fieldErrors.size).to.equal(1)
759+
expect(formEntryError?.fieldErrors).to.have.keys(FormFieldType.Numeric)
760+
761+
observationAttrs.properties.forms = [ invalidFormEntryNull ]
762+
invalid = validateObservation(observationAttrs, mageEvent)
763+
764+
expect(invalid.formEntryErrors.length).to.equal(1)
765+
formEntryError = new Map(invalid.formEntryErrors).get(0)
766+
expect(formEntryError).to.exist
767+
expect(formEntryError?.fieldErrors.size).to.equal(1)
768+
expect(formEntryError?.fieldErrors).to.have.keys(FormFieldType.Numeric)
769+
770+
observationAttrs.properties.forms = [ validFormEntry ]
771+
invalid = validateObservation(observationAttrs, mageEvent)
772+
773+
expect(invalid.hasErrors).to.be.false
774+
})
775+
713776
describe('attachment validation', function() {
714777

715778
let field: FormField

0 commit comments

Comments
 (0)