---
title: 'Validation Documentation'
---

import { Meta } from '@storybook/addon-docs';

<Meta title="Components/Validation/Documentation" />

# Validation Documentation

*Located under: Components/Validation/Documentation*

# USA Validation Component

- [USWDS Form Validation](https://designsystem.digital.gov/components/form-validation/)

## USWDS Documentation

- **USWDS Form Validation**: https://designsystem.digital.gov/components/form-validation/
- **USWDS Error Messages**: https://designsystem.digital.gov/components/form-validation/#error-messages
- **USWDS Success States**: https://designsystem.digital.gov/components/form-validation/#success-states
- **Form Validation Accessibility**: https://designsystem.digital.gov/components/form-validation/#accessibility


## Interactive Examples

You can see interactive examples of this component in the main component story: **Components/Validation**

## Usage Examples

```html

<usa-validation
  label="Email Address"
  hint="Enter a valid email address"
  type="email"
></usa-validation>
```

```html

<usa-validation
  label="Password"
  hint="Must be at least 8 characters"
  type="password"
  name="password"
></usa-validation>

<script>
const passwordField = document.querySelector('usa-validation[name="password"]');
passwordField.rules = [
  \{ type: 'required', message: 'Password is required' \},
  \{ type: 'minlength', value: 8, message: 'Must be at least 8 characters' \}
];
</script>
```

```html

<!-- Text Input -->
<usa-validation
  label="Full Name"
  inputType="input"
  type="text"
></usa-validation>

<!-- Textarea -->
<usa-validation
  label="Comments"
  inputType="textarea"
  rows="5"
></usa-validation>

<!-- Select -->
<usa-validation
  label="State"
  inputType="select"
></usa-validation>

<script>
const stateField = document.querySelector('usa-validation[label="State"]');
stateField.options = [
  \{ value: 'AL', text: 'Alabama' \},
  \{ value: 'CA', text: 'California' \},
  \{ value: 'FL', text: 'Florida' \}
];
</script>
```

```html

<usa-validation
  label="Social Security Number"
  hint="Format: XXX-XX-XXXX"
  placeholder="123-45-6789"
></usa-validation>

<script>
element.rules = [
  \{ type: 'required', message: 'SSN is required' \},
  \{ 
    type: 'pattern', 
    value: '^\\d\{3\}-\\d\{2\}-\\d\{4\}$',
    message: 'SSN must be in format: XXX-XX-XXXX' 
  \}
];
</script>
```

```html

<usa-validation
  label="Federal Employee ID"
  hint="Two letters followed by 8 numbers"
  placeholder="AB12345678"
></usa-validation>

<script>
element.rules = [
  \{ type: 'required', message: 'Employee ID is required' \},
  \{ 
    type: 'pattern', 
    value: '^[A-Z]\{2\}\\d\{8\}$',
    message: 'Employee ID must be 2 letters followed by 8 numbers' 
  \}
];
</script>
```

```html

<usa-validation
  label="Government Email"
  hint="Must be a .gov or .mil email address"
  type="email"
  placeholder="name@agency.gov"
></usa-validation>

<script>
element.rules = [
  \{ type: 'required', message: 'Government email is required' \},
  \{ 
    type: 'custom',
    message: 'Must use a .gov or .mil email address',
    validator: (value) => /^[^\\s@]+@[^\\s@]+\\.(gov|mil)$/.test(value)
  \}
];
</script>
```

```html

<usa-validation
  label="ZIP Code"
  hint="Enter 5-digit ZIP or ZIP+4 format"
  placeholder="12345 or 12345-6789"
></usa-validation>

<script>
element.rules = [
  \{ type: 'required', message: 'ZIP code is required' \},
  \{ 
    type: 'pattern', 
    value: '^\\d\{5\}(-\\d\{4\})?$',
    message: 'ZIP code must be 5 digits or ZIP+4 format' 
  \}
];
</script>
```

```html

<form id="government-form">
  <usa-validation
    label="Applicant Name"
    hint="Enter your full legal name"
    name="applicant-name"
  ></usa-validation>
  
  <usa-validation
    label="Contact Email"
    type="email"
    name="contact-email"
  ></usa-validation>
  
  <button type="submit" class="usa-button">Submit Application</button>
</form>

<script>
// Set validation rules
document.querySelectorAll('usa-validation').forEach(field => \{
  if (field.name === 'applicant-name') \{
    field.rules = [
      \{ type: 'required', message: 'Name is required' \},
      \{ type: 'minlength', value: 2, message: 'Name must be at least 2 characters' \}
    ];
  \}
  
  if (field.name === 'contact-email') \{
    field.rules = [
      \{ type: 'required', message: 'Email is required' \},
      \{ type: 'email', message: 'Please enter a valid email' \}
    ];
  \}
\});

// Form submission validation
document.getElementById('government-form').addEventListener('submit', (e) => \{
  e.preventDefault();
  
  const fields = document.querySelectorAll('usa-validation');
  let allValid = true;
  const formData = \{\};
  
  fields.forEach(field => \{
    const result = field.validate();
    if (!result.isValid) \{
      allValid = false;
      field.focus(); // Focus first invalid field
      return;
    \}
    formData[field.name] = field.value;
  \});
  
  if (allValid) \{
    console.log('Form is valid, submitting:', formData);
    // Proceed with form submission
  \}
\});
</script>
```


## API Reference


### Properties

| Property | Type | Default | Description |
|----------|------|---------|-------------|
| `value` | string | '' | Current input value |
| `label` | string | 'Input with validation' | Field label text |
| `hint` | string | '' | Helper text displayed below label |
| `name` | string | 'validation-input' | Field name and ID |
| `inputType` | 'input' \\| 'textarea' \\| 'select' | 'input' | Type of input control |
| `type` | string | 'text' | HTML input type (text, email, password, etc.) |
| `options` | `Array<\{value: string, text: string\}>` | [] | Options for select input |
| `rows` | number | 3 | Number of rows for textarea |
| `placeholder` | string | '' | Placeholder text |
| `disabled` | boolean | false | Disable the input field |
| `readonly` | boolean | false | Make the input read-only |
| `rules` | ValidationRule[] | [] | Array of validation rules |
| `validateOnInput` | boolean | true | Validate while typing |
| `validateOnBlur` | boolean | true | Validate when field loses focus |
| `showSuccessState` | boolean | true | Show green success state for valid fields |



## Accessibility Features

- ✅ **WCAG 2.1 AA Compliant**: Meets all accessibility standards
- ✅ **Keyboard Navigation**: Full keyboard support
- ✅ **Screen Reader**: Proper ARIA implementation
- ✅ **Focus Management**: Clear focus indicators
- ✅ **High Contrast**: Meets color contrast requirements



## Testing Coverage

**Last Updated**: 2025-09-07  
**Test Coverage**: 70%  
**Accessibility Score**: WCAG AA Compliant ✅



### Unit Tests (Vitest)

✅ **53 unit tests** implemented

- Component rendering, properties, events, and methods tested

### ♿ Accessibility Tests

✅ **ARIA attributes and roles tested**
✅ **Keyboard navigation tested**
✅ **Focus management tested**
✅ **Disabled state accessibility tested**

### 🖱️ Interactive Tests (Cypress)

⚠️ **Interactive tests needed**

- User interactions (click, focus, keyboard)
- Form integration testing
- State transition validation

### 📱 Responsive & Visual Tests

✅ **Storybook stories** available

- 16 visual test scenarios
- Disabled state visually tested
- Error state visually tested

### 🔧 E2E Integration Tests

⚠️ **E2E tests recommended** for complex workflows

## 📊 Detailed Accessibility Compliance

### WCAG 2.1 AA Requirements

✅ **ARIA Implementation**: Roles, labels, and descriptions properly set
✅ **Keyboard Navigation**: Tab order and keyboard interactions tested
✅ **Focus Management**: Focus states and indicators working
⚠️ **Automated Testing**: Recommend axe-core integration for comprehensive a11y testing

### Screen Reader Compatibility

- ⚠️ **Manual testing needed** with VoiceOver, NVDA, and JAWS
- Component should announce purpose, state, and interactions clearly
- Content should be logically structured for screen readers

## 🚨 Testing Gaps & Recommendations

### ✅ Good Coverage

Component has solid test coverage. Continue maintaining and expanding as needed.




## Changelog

## [Unreleased]


### Changed
- remove 41 redundant SCSS files with only basic host styles
### Added

- implement comprehensive per-component changelog system

## [1.0.0] - 2025-09-07

### Added

- Initial validation component implementation
- USWDS styling and design system compliance
- Accessibility features and ARIA support
- Comprehensive test coverage
- Storybook stories and documentation
- TypeScript definitions and type safety

### Dependencies

- lit: ^3.3.1
- USWDS classes: .usa-validation

---

_This changelog is automatically updated by git hooks and scripts._
_See `docs/CHANGELOG_MANAGEMENT.md` for more information._



## Implementation Notes

- Built with Lit Element for optimal performance
- Uses light DOM for maximum compatibility
- Extracts only necessary USWDS CSS
- Fully accessible and keyboard navigable
- Screen reader compatible

---

*This documentation is automatically generated from component files and synchronized with the latest changes.*
