Skip to content

Commit fdfc826

Browse files
Simplify validation data attribute flags into data-validation=
* To simplify the number of attribute flags that need to be remembered to use this library effectively, we can simplify the `data-*-validation` attributes into a single `data-validation=` attribute, where the value of the attribute states its behavior: > `data-validation="input"` > `data-validation="change"` > `data-validation="focusout"` > `data-validation="skip"` Co-authored-by: Konnor Rogers <konnor5456@gmail.com>
1 parent bdba456 commit fdfc826

File tree

8 files changed

+50
-50
lines changed

8 files changed

+50
-50
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ The following terms are used for this package:
143143
The following data attributes are used by this package:
144144

145145
- Inputs
146-
- Validation Event Handling flags. This package does not provide the validations, but does provide helper functions for checks if an event handler should proceed.
147-
- `data-live-validation`: This input should use live validation (eg: `input`).
148-
- `data-change-validation`: This input should use change validation (eg: `change`)
149-
- `data-focusout-validation`: This input should use `focusout` validation
150-
- `data-skip-validation`: This input should skip any validations
146+
- Validation Event Handling flags. This package does not provide the validations, but does provide helper functions for checks if an event handler should proceed by checking the value of `data-validation`:
147+
- `data-validation="input"`: This input should use `input` validation
148+
- `data-validation="change"`: This input should use `change` validation
149+
- `data-validation="focusout"`: This input should use `focusout` validation
150+
- `data-validation="skip"`: This input should skip any validations
151151
- `data-initial-load-errors`: If present, this input has initial load errors, which should not be cleared out when reflecting the constraint validation for the initial load.
152152
- `data-error-container`: Marks an element as an error container
153153
- Error list items

src/element-utilities.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,39 @@ export function generateValidationMessage(element) {
1111
}
1212

1313
/**
14-
* Checks if the given `element` has the `data-live-validation` attribute
14+
* Checks if the given `element` has `data-validation == "input"`
1515
* @param {Element} element
16-
* @return {boolean} Returns true if the element does not have the `data-live-validation` attribute`
16+
* @return {boolean} Returns true if the element does not have `data-validation == "input"`
1717
*/
1818
export function skipInputValidation(element) {
19-
return !element.hasAttribute(`data-live-validation`)
19+
return !(element.getAttribute(`data-validation`) === "input")
2020
}
2121

2222
/**
23-
* Checks if the given `element` has the `data-change-validation` attribute
23+
* Checks if the given `element` has `data-validation == "change"`
2424
* @param {Element} element
25-
* @return {boolean} Returns true if the element does not have the `data-change-validation` attribute`
25+
* @return {boolean} Returns true if the element does not have `data-validation == "change"`
2626
*/
2727
export function skipChangeValidation(element) {
28-
return !element.hasAttribute(`data-change-validation`)
28+
return !(element.getAttribute(`data-validation`) === "change")
2929
}
3030

3131
/**
32-
* Checks if the given `element` has the `data-focusout-validation` attribute
32+
* Checks if the given `element` has `data-validation == "focusout"`
3333
* @param {Element} element
34-
* @return {boolean} Returns true if the element does not have the `data-focusout-validation` attribute`
34+
* @return {boolean} Returns true if the element does not have `data-validation == "focusout"`
3535
*/
3636
export function skipFocusoutValidation(element) {
37-
return !element.hasAttribute(`data-focusout-validation`)
37+
return !(element.getAttribute(`data-validation`) === "focusout")
3838
}
3939

4040
/**
41-
* Checks if the given `element` has the `data-skip-validation` attribute
41+
* Checks if the given `element` given `element` has `data-validation == "skip"`
4242
* @param {Element} element
43-
* @return {boolean} Returns true if the element has the `data-skip-validation` attribute`
43+
* @return {boolean} Returns true if the element has `data-validation == "change"`
4444
*/
4545
export function skipValidation(element) {
46-
return element.hasAttribute(`data-skip-validation`)
46+
return (element.getAttribute(`data-validation`) === "skip")
4747
}
4848

4949
/**

src/error-handling-element.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
liveInputValidationEventHandler,
2+
inputValidationEventHandler,
33
focusoutValidationEventHandler,
44
validateFormSubmitEventHandler
55
} from './event-handlers.js'
@@ -44,7 +44,7 @@ export class ErrorHandlingElement extends HTMLElement {
4444
* Prepares the custom element to be connected to the page.
4545
*
4646
* Attaches the following event handlers to the {@link ErrorHandlingElement#form}:
47-
* - {@link liveInputValidationEventHandler}
47+
* - {@link inputValidationEventHandler}
4848
* - {@link focusoutValidationEventHandler}
4949
* - {@link validateFormSubmitEventHandler}
5050
*
@@ -56,7 +56,7 @@ export class ErrorHandlingElement extends HTMLElement {
5656
if(!this.isConnected){ return }
5757

5858
this.form.setAttribute('novalidate', '')
59-
this.form.addEventListener('input', liveInputValidationEventHandler)
59+
this.form.addEventListener('input', inputValidationEventHandler)
6060
this.form.addEventListener('focusout', focusoutValidationEventHandler)
6161
this.form.addEventListener('submit', validateFormSubmitEventHandler)
6262

src/event-handlers.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ export function validateFormSubmitEventHandler(event) {
3535

3636
/**
3737
* Calls {@link reflectConstraintValidationForElement} if the `event.target` has
38-
* `data-live-validation` and does not have `data-skip-validation`
38+
* `data-validation="input"` and does not have `data-skip-validation`
3939
*
4040
* @param {Event} event
4141
*/
42-
export function liveInputValidationEventHandler(event) {
42+
export function inputValidationEventHandler(event) {
4343
let element = event.target
4444
if(skipInputValidation(element)){ return }
4545
if(skipValidation(element)){ return }
@@ -48,7 +48,7 @@ export function liveInputValidationEventHandler(event) {
4848

4949
/**
5050
* Calls {@link reflectConstraintValidationForElement} if the `event.target` has
51-
* `data-focusout-validation` and does not have `data-skip-validation`
51+
* `data-validation="focusout"` and does not have `data-skip-validation`
5252
* @param {Event} event
5353
*/
5454
export function focusoutValidationEventHandler(event) {

test/element-utilities.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,48 +23,48 @@ suite('Element Utilities', async () => {
2323

2424
test(`skipInputValidation`, async() => {
2525
const el = await fixture(html`
26-
<input type="text" data-live-validation>
26+
<input type="text" data-validation="input">
2727
`);
2828

2929
assert.equal(false, ElementUtils.skipInputValidation(el))
3030

31-
el.removeAttribute(`data-live-validation`)
31+
el.setAttribute(`data-validation`, `change`)
3232

3333
assert.equal(true, ElementUtils.skipInputValidation(el))
3434
})
3535

3636
test(`skipChangeValidation`, async() => {
3737
const el = await fixture(html`
38-
<input type="text" data-change-validation>
38+
<input type="text" data-validation="change">
3939
`);
4040

4141
assert.equal(false, ElementUtils.skipChangeValidation(el))
4242

43-
el.removeAttribute(`data-change-validation`)
43+
el.setAttribute(`data-validation`, `input`)
4444

4545
assert.equal(true, ElementUtils.skipChangeValidation(el))
4646
})
4747

4848
test(`skipFocusoutValidation`, async() => {
4949
const el = await fixture(html`
50-
<input type="text" data-focusout-validation>
50+
<input type="text" data-validation="focusout">
5151
`);
5252

5353
assert.equal(false, ElementUtils.skipFocusoutValidation(el))
5454

55-
el.removeAttribute(`data-focusout-validation`)
55+
el.setAttribute(`data-validation`, `input`)
5656

5757
assert.equal(true, ElementUtils.skipFocusoutValidation(el))
5858
})
5959

6060
test(`skipValidation`, async() => {
6161
const el = await fixture(html`
62-
<input type="text" data-skip-validation>
62+
<input type="text" data-validation="skip">
6363
`);
6464

6565
assert.equal(true, ElementUtils.skipValidation(el))
6666

67-
el.removeAttribute(`data-skip-validation`)
67+
el.setAttribute(`data-validation`, `input`)
6868

6969
assert.equal(false, ElementUtils.skipValidation(el))
7070
})

test/event-handlers.test.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,43 @@ import { html, fixture, assert } from '@open-wc/testing';
33
import * as EventHandlers from '@practical-computer/error-handling/event-handlers';
44

55
suite('Event Handlers', async () => {
6-
test(`liveInputValidationEventHandler: does nothing if no data-live-validation`, async() => {
6+
test(`inputValidationEventHandler: does nothing if no data-validation`, async() => {
77
const input = await fixture(html`
88
<input type="email" required>
99
`)
1010

11-
input.addEventListener(`test-event`, EventHandlers.liveInputValidationEventHandler)
11+
input.addEventListener(`test-event`, EventHandlers.inputValidationEventHandler)
1212

1313
input.dispatchEvent(new CustomEvent(`test-event`))
1414

1515
assert.equal(false, input.hasAttribute(`aria-invalid`))
1616
})
1717

18-
test(`liveInputValidationEventHandler: fires if data-live-validation`, async() => {
18+
test(`inputValidationEventHandler: fires if data-validation="input"`, async() => {
1919
const input = await fixture(html`
20-
<input type="email" required data-live-validation>
20+
<input type="email" required data-validation="input">
2121
`)
2222

23-
input.addEventListener(`test-event`, EventHandlers.liveInputValidationEventHandler)
23+
input.addEventListener(`test-event`, EventHandlers.inputValidationEventHandler)
2424

2525
input.dispatchEvent(new CustomEvent(`test-event`))
2626

2727
assert.equal("true", input.getAttribute(`aria-invalid`))
2828
})
2929

30-
test(`liveInputValidationEventHandler: does nothing if data-live-validation but data-skip-validation`, async() => {
30+
test(`inputValidationEventHandler: does nothing if data-validation="skip"`, async() => {
3131
const input = await fixture(html`
32-
<input type="email" required data-live-validation data-skip-validation>
32+
<input type="email" required data-validation="skip">
3333
`)
3434

35-
input.addEventListener(`test-event`, EventHandlers.liveInputValidationEventHandler)
35+
input.addEventListener(`test-event`, EventHandlers.inputValidationEventHandler)
3636

3737
input.dispatchEvent(new CustomEvent(`test-event`))
3838

3939
assert.equal(false, input.hasAttribute(`aria-invalid`))
4040
})
4141

42-
test(`focusoutValidationEventHandler: does nothing if no data-focusout-validation`, async() => {
42+
test(`focusoutValidationEventHandler: does nothing if no data-validation="input"`, async() => {
4343
const input = await fixture(html`
4444
<input type="email" required>
4545
`)
@@ -51,9 +51,9 @@ suite('Event Handlers', async () => {
5151
assert.equal(false, input.hasAttribute(`aria-invalid`))
5252
})
5353

54-
test(`focusoutValidationEventHandler: fires if data-focusout-validation-validation`, async() => {
54+
test(`focusoutValidationEventHandler: fires if data-validation="focusout"`, async() => {
5555
const input = await fixture(html`
56-
<input type="email" required data-focusout-validation>
56+
<input type="email" required data-validation="focusout">
5757
`)
5858

5959
input.addEventListener(`test-event`, EventHandlers.focusoutValidationEventHandler)
@@ -63,9 +63,9 @@ suite('Event Handlers', async () => {
6363
assert.equal("true", input.getAttribute(`aria-invalid`))
6464
})
6565

66-
test(`focusoutValidationEventHandler: does nothing if data-focusout-validation but data-skip-validation`, async() => {
66+
test(`focusoutValidationEventHandler: does nothing if data-validation="skip"`, async() => {
6767
const input = await fixture(html`
68-
<input type="email" required data-focusout-validation data-skip-validation>
68+
<input type="email" required data-validation="skip">
6969
`)
7070

7171
input.addEventListener(`test-event`, EventHandlers.focusoutValidationEventHandler)

test/fieldset/minimum-field-values-fieldset-validation-element.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ suite('minimum-field-values-fieldset-validation-element', async () => {
5151
fieldset="terms-fieldset"
5252
field-name="terms"
5353
error-container-aria="terms-fieldset-errors-aria"
54-
data-change-validation
54+
data-validation="change"
5555
>
5656
<input type="checkbox" name="terms" id="privacy" value="privacy" required>
5757
<input type="checkbox" name="terms" id="service" value="service" required>
@@ -121,7 +121,7 @@ suite('minimum-field-values-fieldset-validation-element', async () => {
121121
fieldset="terms-fieldset"
122122
field-name="terms"
123123
error-container-aria="terms-fieldset-errors-aria"
124-
data-focusout-validation
124+
data-validation="focusout"
125125
>
126126
<input type="checkbox" name="terms" id="privacy" value="privacy" required>
127127
<input type="checkbox" name="terms" id="service" value="service" required>

test/fieldset/util.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ suite('Event Utils: skipFieldsetChangeEvent', async () => {
152152
test(`skips validation`, async() => {
153153
const container = await fixture(html`
154154
<div>
155-
<fieldset data-change-validation data-skip-validation>
155+
<fieldset data-validation="skip">
156156
<input type="checkbox">
157157
</fieldset>
158158
</div>
@@ -175,7 +175,7 @@ suite('Event Utils: skipFieldsetChangeEvent', async () => {
175175
test(`follows through on change validation`, async() => {
176176
const container = await fixture(html`
177177
<div>
178-
<fieldset data-change-validation>
178+
<fieldset data-validation="change">
179179
<input type="checkbox">
180180
</fieldset>
181181
</div>
@@ -251,7 +251,7 @@ suite('Event Utils: skipFieldsetFocusoutEvent', async () => {
251251
test(`skips validation`, async() => {
252252
const container = await fixture(html`
253253
<div>
254-
<fieldset data-focusout-validation data-skip-validation>
254+
<fieldset data-validation="skip">
255255
<input type="checkbox">
256256
</fieldset>
257257
@@ -277,7 +277,7 @@ suite('Event Utils: skipFieldsetFocusoutEvent', async () => {
277277
test(`follows through on validation`, async() => {
278278
const container = await fixture(html`
279279
<div>
280-
<fieldset data-focusout-validation>
280+
<fieldset data-validation="focusout">
281281
<input type="checkbox">
282282
</fieldset>
283283

0 commit comments

Comments
 (0)