-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d97948
commit ed99219
Showing
10 changed files
with
84 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,55 @@ | ||
import {ContactProperty} from '../config'; | ||
import {IValidator} from './validation'; | ||
import ValidatorString from './validator-string'; | ||
import ValidatorSelectOne from './validator-select_one'; | ||
|
||
export default class ValidatorSelectMultiple implements IValidator { | ||
isValid(input: string, property: ContactProperty): boolean { | ||
DELIMITER = ' '; | ||
|
||
isValid(input: string, property: ContactProperty): boolean | string { | ||
// Verify property.parameter is an object and is not null | ||
if (!property?.parameter || typeof property.parameter !== 'object') { | ||
throw new TypeError(`Expected property "parameter" to be an object.`); | ||
} | ||
const validValues = Object.keys(property.parameter); | ||
let selectedValues; | ||
if (Array.isArray(input)) { | ||
selectedValues = input; | ||
} else { | ||
selectedValues = input.split(' ').map(value => value.trim()).filter(value => value !== ''); | ||
throw new TypeError(`Expected attribute "parameter" on property ${property.property_name} to be an object.`); | ||
} | ||
const invalidValues = selectedValues.filter(value => !validValues.includes(value)); | ||
|
||
const selectOneValidator = new ValidatorSelectOne(); | ||
const stringValidator = new ValidatorString(); | ||
|
||
const selectedValues = this.parseInput(input, stringValidator); | ||
const invalidValues = selectedValues.filter( | ||
value => !selectOneValidator.isValid(value, property) | ||
); | ||
|
||
if (invalidValues.length > 0) { | ||
throw new Error(`Invalid values: ${invalidValues.join(', ')}`); | ||
return `Invalid values: ${invalidValues.join(', ')}`; | ||
} | ||
|
||
// Check if any values are missing and property is required | ||
if (selectedValues.length === 0 && property.required) { | ||
throw new Error('Value is required'); | ||
return 'Value is required'; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
format(input: string): string { | ||
return Array.isArray(input) ? input.join(' ') : input; | ||
return Array.isArray(input) ? input.join(this.DELIMITER) : input; | ||
} | ||
|
||
get defaultError(): string { | ||
return 'Invalid input'; | ||
return `Invalid input. Please use 'space' as delimiter.`; | ||
} | ||
|
||
private parseInput(input: string|string[], stringValidator: ValidatorString): string[] { | ||
if (Array.isArray(input)) { | ||
return input; | ||
} | ||
|
||
// If input is a string, split it by delimiter | ||
return input | ||
.split(this.DELIMITER) | ||
.map(value => stringValidator.format(value)) | ||
.filter(value => value); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters