-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
12 changed files
with
82 additions
and
22 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
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
18 changes: 18 additions & 0 deletions
18
model/src/value-models/choice/choice-value-model-configuration.ts
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,18 @@ | ||
export interface ChoiceValueModelConfiguration<TValue extends string = string> { | ||
/** | ||
* Label. If not provided, the label is generated from the property name. | ||
*/ | ||
label?: string; | ||
/** | ||
* Supported choices. | ||
*/ | ||
choices: TValue[]; | ||
/** | ||
* Default value. | ||
*/ | ||
defaultValue?: TValue; | ||
/** | ||
* Custom editor ID. | ||
*/ | ||
editorId?: string; | ||
} |
20 changes: 20 additions & 0 deletions
20
model/src/value-models/choice/choice-value-model-validator.spec.ts
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,20 @@ | ||
import { createValueContextStub } from '../../test-tools/value-context-stub'; | ||
import { ChoiceValueModel } from './choice-value-model'; | ||
import { ChoiceValueModelConfiguration } from './choice-value-model-configuration'; | ||
import { choiceValueModelValidator } from './choice-value-model-validator'; | ||
|
||
describe('choiceValueModelValidator', () => { | ||
it('returns correct response', () => { | ||
const configuration: ChoiceValueModelConfiguration = { | ||
choices: ['x', 'y'] | ||
}; | ||
|
||
const context1 = createValueContextStub<ChoiceValueModel>('z', configuration); | ||
const error1 = choiceValueModelValidator(context1); | ||
expect(error1?.$).toBe('Value is not supported'); | ||
|
||
const context2 = createValueContextStub<ChoiceValueModel>('x', configuration); | ||
const error2 = choiceValueModelValidator(context2); | ||
expect(error2).toBe(null); | ||
}); | ||
}); |
12 changes: 12 additions & 0 deletions
12
model/src/value-models/choice/choice-value-model-validator.ts
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,12 @@ | ||
import { ValueContext } from '../../context'; | ||
import { createValidationSingleError, ValidationResult } from '../../model'; | ||
import { ChoiceValueModel } from './choice-value-model'; | ||
|
||
export function choiceValueModelValidator(context: ValueContext<ChoiceValueModel>): ValidationResult { | ||
const value = context.getValue(); | ||
const configuration = context.model.configuration; | ||
if (!configuration.choices.includes(value)) { | ||
return createValidationSingleError(context.i18n('choice.notSupportedValue', 'Value is not supported')); | ||
} | ||
return null; | ||
} |
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 +1,2 @@ | ||
export * from './choice-value-model-configuration'; | ||
export * from './choice-value-model'; |