-
Notifications
You must be signed in to change notification settings - Fork 0
Apply validation functions to mapping. #192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
3927fb3
579df1d
2ab7285
e3a61c6
32334fc
6244e87
76130c2
1062b2a
edef69e
009e636
8ebbd5b
666d10d
7e82f2d
b2877d6
f2cbe70
e3ad256
6a13173
9596c79
f46bd9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,14 +5,13 @@ | |
| // An attribute type is a function from an input value to a result. | ||
|
|
||
| class AttributeMappingResult { | ||
| constructor(value, warnings, errors) { | ||
| constructor(value, errors) { | ||
| this.value = value; | ||
| this.warnings = warnings || []; | ||
| this.errors = errors; | ||
| this.errors = errors || []; | ||
| } | ||
|
|
||
| get valid() { | ||
| return !this.errors; | ||
| return this.errors.length == 0; | ||
| } | ||
|
|
||
| get empty() { | ||
|
|
@@ -22,20 +21,20 @@ class AttributeMappingResult { | |
|
|
||
| // These helpers define the three kinds of results: | ||
|
|
||
| function emptyMapping(warnings) { | ||
| return new AttributeMappingResult(undefined, warnings); | ||
| function emptyMapping(errors) { | ||
| return new AttributeMappingResult(undefined, errors); | ||
| } | ||
|
|
||
| function successfulMapping(outputValue, warnings) { | ||
| return new AttributeMappingResult(outputValue, warnings); | ||
| function successfulMapping(outputValue, errors) { | ||
| return new AttributeMappingResult(outputValue, errors); | ||
| } | ||
|
|
||
| function failedMapping(warnings, errors) { | ||
| return new AttributeMappingResult(undefined, warnings, errors); | ||
| function failedMapping(errors) { | ||
| return new AttributeMappingResult(undefined, errors); | ||
| } | ||
|
|
||
| // Create an optional version of an existing type, that allows empty strings or | ||
| // undefined values and maps then to "undefined", and converts any validation errors to warnings | ||
| // undefined values and maps then to "undefined" | ||
| exports.optionalType = (baseType) => { | ||
| return (inputValue) => { | ||
| if (inputValue !== undefined && inputValue != "") { | ||
|
|
@@ -44,7 +43,7 @@ exports.optionalType = (baseType) => { | |
| return result; | ||
| } else { | ||
| // Convert any errors into warnings, as this is an optional field | ||
| return emptyMapping(result.warnings.concat(result.errors)); | ||
| return emptyMapping(result.errors); | ||
| } | ||
| } | ||
| else { | ||
|
|
@@ -60,7 +59,7 @@ exports.requiredType = (baseType) => { | |
| return baseType(inputValue); | ||
| } | ||
| else { | ||
| return failedMapping([], ["A value must be provided"]); | ||
| return failedMapping(["A value must be provided"]); | ||
| } | ||
| }; | ||
| } | ||
|
|
@@ -74,19 +73,28 @@ exports.basicStringType = (inputValue) => { | |
| exports.basicNumberType = (inputValue) => { | ||
| const result = parseFloat(inputValue); | ||
| if (isNaN(result)) { | ||
| return failedMapping([], ["This is not a valid number"]); | ||
| return failedMapping(["This is not a valid number"]); | ||
| } else { | ||
| return successfulMapping(result); | ||
| } | ||
| }; | ||
|
|
||
| exports.typeFromName = (name) => { | ||
| if (!name) return null | ||
| exports.mapperForField = (field) => { | ||
| if (!field) return null | ||
|
|
||
| switch (name.toLowerCase()) { | ||
| let m = exports.basicStringType; | ||
|
|
||
| switch (field.type.toLowerCase()) { | ||
| case "number": | ||
| return exports.basicNumberType | ||
| m = exports.basicNumberType | ||
| break | ||
| default: | ||
| return exports.basicStringType | ||
| m = exports.basicStringType | ||
| } | ||
|
|
||
| if (field.required) { | ||
| return exports.requiredType(m) | ||
| } | ||
|
|
||
| return exports.optionalType(m) | ||
|
Comment on lines
-83
to
+99
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something here doesn't seem to be working. If I have config: {
"serviceName": "Upload monthly pension return",
"fields": [
{
"name": "Title",
"type": "text",
"required": false
},
{
"name": "First name",
"type": "text",
"required": false
},
{
"name": "Surname",
"type": "text",
"required": false
},
{
"name": "Employee number",
"type": "text",
"required": true
},
{
"name": "Employment start date",
"type": "text",
"required": false
},
{
"name": "Salary",
"type": "text",
"required": true
},
{
"name": "Contribution percentage",
"type": "text",
"required": true
},
{
"name": "Payment date",
"type": "text",
"required": false
}
],
"uploadPath": ""
}and I upload this CSV and map the columns in the obvious way: Employee number,Title,First name,Surname,Employment start date,Salary,Contribution percentage,Payment date
1,Mr,Jon,Arbuckle,2025-01-01,,,2025-04-01
2,Mr,Odie,Arbuckle,2025-02-01,123,12%,2025-04-01Then I am told that I have uploaded two rows and there are no errors. But there should be errors because one of the rows is missing values for required fields. More tests needed?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolved in 7e82f2d, tests to follow. |
||
| } | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the anchor here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in the design system it's intended to jump to the anchor where the error occurs, but I think I just forgot to go back and implement that by adding ids to the fields. Will sort it..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in e3ad256 but not sure scrolling down the page without a visual indicator on the erroring row is that helpful, but not sure I know how to visually indicate the error in a table.