Skip to content
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

fix(validate-mixin): determine required or result validators based on characteristics #2498

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tall-doors-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lion/ui': patch
---

[validate-mixin] determine if a required validator or result validator has been registered based on characteristics
12 changes: 6 additions & 6 deletions packages/ui/components/form-core/src/validate/ValidateMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { AsyncQueue } from '../utils/AsyncQueue.js';
import { pascalCase } from '../utils/pascalCase.js';
import { SyncUpdatableMixin } from '../utils/SyncUpdatableMixin.js';
import { LionValidationFeedback } from './LionValidationFeedback.js';
import { ResultValidator as MetaValidator } from './ResultValidator.js';
import { Unparseable } from './Unparseable.js';
import { Required } from './validators/Required.js';
import { FormControlMixin } from '../FormControlMixin.js';
// eslint-disable-next-line no-unused-vars
import { ResultValidator as MetaValidator } from './ResultValidator.js';
// eslint-disable-next-line no-unused-vars
import { Validator } from './Validator.js';
// TODO: [v1] make all @readOnly => @readonly and actually make sure those values cannot be set

Expand Down Expand Up @@ -134,7 +134,7 @@ export const ValidateMixinImplementation = superclass =>

/**
* Combination of validators provided by Application Developer and the default validators
* @type {Validator[]}
* @type {(Validator | MetaValidator)[]}
* @protected
*/
get _allValidators() {
Expand Down Expand Up @@ -480,9 +480,9 @@ export const ValidateMixinImplementation = superclass =>
const asyncValidators = /** @type {Validator[]} */ [];

for (const v of this._allValidators) {
if (v instanceof MetaValidator) {
metaValidators.push(v);
} else if (v instanceof Required) {
if (/** @type {MetaValidator} */ (v)?.executeOnResults) {
metaValidators.push(/** @type {MetaValidator} */ (v));
} else if (/** @type {typeof Validator} */ (v.constructor)?.validatorName === 'Required') {
// Required validator was already handled
} else if (/** @type {typeof Validator} */ (v.constructor).async) {
asyncValidators.push(v);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,121 @@ export function runValidateMixinSuite(customConfig) {
expect(el.hasFeedbackFor).to.deep.equal(['error']);
});

it('determines whether the "Required" validator was already handled by judging the validatorName', async () => {
class BundledValidator extends EventTarget {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you maybe extend from the Validator class here (to set the right example in tesrs as well?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I were to extend from Validator then the instanceof check would have passed too, wanted to illustrate the case where Validator itself gets bundled; if I use BundledRequired which extends BundledValidator, it works too. Do you think this is not representative? 🤔

static ['_$isValidator$'] = true;

static validatorName = '';

constructor() {
super();

this.type = 'error';
}

get config() {
// simplified version of the actual config
return {};
}

execute() {
// simplified version of the action execution
return true;
}

onFormControlConnect() {}

onFormControlDisconnect() {}

async _getMessage() {
// simplified version of the actual logic
return 'You need to enter something.';
}
}

class BundledRequired extends BundledValidator {
static get validatorName() {
return 'Required';
}
}

const el = /** @type {ValidateElement} */ (
await fixture(html`
<${tag}
.validators=${[new BundledRequired()]}
.modelValue=${'myValue'}
>${lightDom}</${tag}>
`)
);

expect(el.hasFeedbackFor).to.deep.equal([]);
});

it('determines whether the passed Validators are ResultValidators judging by the presence of "executeOnResults"', async () => {
class ValidateElementWithSuccessType extends ValidateElement {
static get validationTypes() {
return ['error', 'success'];
}
}

const elTagString = defineCE(ValidateElementWithSuccessType);
const elTag = unsafeStatic(elTagString);

class BundledValidator extends EventTarget {
static ['_$isValidator$'] = true;

static validatorName = '';

constructor() {
super();

this.type = 'error';
}

get config() {
// simplified version of the actual config
return {};
}

execute() {
// simplified version of the action execution
return true;
}

onFormControlConnect() {}

onFormControlDisconnect() {}

async _getMessage() {
// simplified version of the actual logic
return 'Success message.';
}
}

class BundledDefaultSuccess extends BundledValidator {
constructor() {
super();

this.type = 'success';
}

executeOnResults() {
return true;
}
}

const el = /** @type {ValidateElement} */ (
await fixture(html`
<${elTag}
.validators=${[new Required(), new BundledDefaultSuccess()]}
.modelValue=${'myValue'}
>${lightDom}</${elTag}>
`)
);

expect(el.hasFeedbackFor).to.deep.equal(['success']);
});

it('revalidates when ".modelValue" changes', async () => {
const el = /** @type {ValidateElement} */ (
await fixture(html`
Expand Down Expand Up @@ -791,7 +906,7 @@ export function runValidateMixinSuite(customConfig) {
// @ts-expect-error
new EqualsLength(4, { getMessage: () => html`<div id="test123">test</div>` }),
]}" })]}"
.modelValue="${'123'}"
.modelValue="${'123'}"
label="Custom message for validator instance"
></${tag}>
`)
Expand Down