-
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.
feat(#55): add select_one and select_multiple
- Loading branch information
1 parent
ad6acb6
commit f6370ee
Showing
9 changed files
with
107 additions
and
18 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {ContactProperty} from '../config'; | ||
import {IValidator} from './validation'; | ||
|
||
export default class ValidatorSelectMultiple implements IValidator { | ||
isValid(input: string, property: ContactProperty): boolean { | ||
// 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 !== ''); | ||
} | ||
const invalidValues = selectedValues.filter(value => !validValues.includes(value)); | ||
if (invalidValues.length > 0) { | ||
throw new Error(`Invalid values: ${invalidValues.join(', ')}`); | ||
} | ||
if (selectedValues.length === 0 && property.required) { | ||
throw new Error('Value is required'); | ||
} | ||
return true; | ||
} | ||
|
||
format(input: string): string { | ||
return Array.isArray(input) ? input.join(' ') : input; | ||
} | ||
|
||
get defaultError(): string { | ||
return 'Invalid input'; | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {ContactProperty} from '../config'; | ||
import {IValidator} from './validation'; | ||
import {property} from "lodash"; | ||
|
||
export default class ValidatorSelectOne implements IValidator { | ||
isValid(input: string, property: ContactProperty): boolean { | ||
if (input.trim().length === 0 && property.required) { | ||
throw new Error('Value is required'); | ||
} | ||
// Verify property.parameter is an object | ||
if (!property?.parameter || typeof property.parameter !== 'object') { | ||
throw new TypeError(`Expected property "parameter" to be an object.`); | ||
} | ||
const validValues = Object.keys(property.parameter); | ||
return validValues.includes(input.trim()); | ||
} | ||
|
||
format(input: string): string { | ||
return input; | ||
} | ||
|
||
get defaultError(): string { | ||
return 'Invalid value selected'; | ||
} | ||
|
||
} |
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