Altinn / Altinn/app-frontend-react

Schema validation coerces missing numbers to 0

Open
#2,679 2 comments 0 reactions 0 assignees View on GitHub
area/validation kind/bug org/brg status/for-consideration
Dominant language
TypeScript
Stars
21
Forks
33
Avg merge
6d 23h
Merged PRs (30d)
3

Description

### Description of the bug

Slack: https://altinn.slack.com/archives/C02EJ9HKQA3/p1730302631757679

We use `coerceTypes: true` in AJV, this will convert values to the "correct" type before validating. This can be useful if e.g. a number is stored as a string, so that `"12"` will be treated as `12` and will not fail the `type: number` validation. A less fortunate side-effect of this is that it will also coerce `null` to something that is valid for each type. For numbers, `null` -> `0`, which can cause validations to appear when they should not. We use `null` to say that a value is missing, and this should not give an error unless it is `required`.

`coerceTypes: true` was probably necessary previously when we stored all of the form data as strings. This is no longer the case, so we may be able to get rid of this option now. It could easily break things however, so we cannot just remove this without testing extensively in existing apps.

One possible solution is removing `null` values from the datamodel before running the schema validation. It does not seem like we can fix this by messing with other AJV options (`useDefaults: 'empty'` did not change things)

### Steps To Reproduce

.

### Additional Information

_No response_

Contributor guide

Open the contributing guide

Research direction

The issue names no files or tests. Start at the AJV configuration using coerceTypes: true and reproduce validation of a null number; done means missing null values do not trigger type errors unless required, with existing apps checked for regressions.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.