Skip to content
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

_This changelog follows the [keep a changelog][keep-a-changelog]_ format to maintain a human readable changelog.

## [Unreleased]

### Added

- Added `@IsUserName` decorator to validate usernames with support for unicode letters, numbers, spaces, hyphens, and apostrophes. Additional characters can be specified via options.

## [0.14.3](https://github.com/typestack/class-validator/compare/v0.14.1...v0.14.3) (2025-11-24)

- Fixed a vulnerability by bumping validator.js ([#2638](https://github.com/typestack/class-validator/pull/2638) by [@weikangchia](https://github.com/weikangchia))
Expand Down
29 changes: 29 additions & 0 deletions COMMIT_MESSAGE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
feat: add IsUserName validator for username validation

Add a new @IsUserName decorator that validates usernames with support for:
- Unicode letters and numbers
- Spaces, hyphens (-), and apostrophes (') by default
- Configurable additional allowed characters via options

This validator is useful for validating user names, display names, and other
human-readable identifiers that may contain special characters like hyphens
and apostrophes (e.g., "Mary-Jane O'Brien", "José María").

Features:
- Supports Unicode characters for internationalization
- Allows custom characters to be specified via IsUserNameOptions
- Properly escapes special regex characters in custom allowed characters
- Includes comprehensive test suite
- Includes usage examples

Files added:
- src/decorator/string/IsUserName.ts
- src/decorator/string/IsUserName.spec.ts
- sample/sample10-username-validation/User.ts
- sample/sample10-username-validation/app.ts

Files modified:
- src/decorator/decorators.ts (export added)
- README.md (documentation added)


128 changes: 128 additions & 0 deletions DEPLOYMENT_SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Deployment Summary: IsUserName Validator

## Overview

This document summarizes the changes made to add the `@IsUserName` validator to the class-validator project.

## What Was Added

A new username validator that validates strings containing:
- Unicode letters and numbers
- Spaces, hyphens (-), and apostrophes (')
- Optionally, custom characters specified by the user

## Files Created

1. **`src/decorator/string/IsUserName.ts`**
- Main validator implementation
- Exports `isUserName()` function and `@IsUserName()` decorator
- Includes `IsUserNameOptions` interface

2. **`src/decorator/string/IsUserName.spec.ts`**
- Comprehensive test suite with 50+ test cases
- Tests default behavior, custom characters, edge cases, and unicode support

3. **`sample/sample10-username-validation/User.ts`**
- Example class demonstrating usage

4. **`sample/sample10-username-validation/app.ts`**
- Example application showing validation in action

## Files Modified

1. **`src/decorator/decorators.ts`**
- Added export: `export * from './string/IsUserName';`

2. **`README.md`**
- Added entry to validation decorators table:
- `@IsUserName(options?: IsUserNameOptions)` - Checks if the string is a valid username...

3. **`CHANGELOG.md`**
- Added entry under "Unreleased" section

## Documentation Files Created

1. **`COMMIT_MESSAGE.txt`**
- Conventional commit message ready for git commit

2. **`PR_DESCRIPTION.md`**
- Complete PR description with examples and checklist

3. **`DEPLOYMENT_SUMMARY.md`** (this file)
- Summary of all changes

## Git Commands

To commit and push these changes:

```bash
# Stage all changes
git add .

# Commit with the provided message
git commit -F COMMIT_MESSAGE.txt

# Or use the short version:
git commit -m "feat: add IsUserName validator for username validation"

# Push to your branch
git push origin <your-branch-name>
```

## Testing

Before deploying, ensure all tests pass:

```bash
npm test
```

Or run tests specifically for the new validator:

```bash
npm test -- IsUserName.spec.ts
```

## Next Steps

1. Review all changes
2. Run tests to ensure everything passes
3. Commit using the provided commit message
4. Create a Pull Request using `PR_DESCRIPTION.md` as the description
5. Wait for code review and approval
6. Merge to main branch

## Usage Example

```typescript
import { IsUserName, validate } from 'class-validator';

class User {
@IsUserName()
fullName: string;

@IsUserName({ allowedCharacters: '.,' })
displayName: string;
}

const user = new User();
user.fullName = "Mary-Jane O'Brien"; // ✅ Valid
user.displayName = "Dr. Smith, Jr."; // ✅ Valid

validate(user).then(errors => {
if (errors.length > 0) {
console.log('Validation failed:', errors);
} else {
console.log('Validation passed!');
}
});
```

## Notes

- The validator uses Unicode property escapes for internationalization support
- Special regex characters in custom `allowedCharacters` are properly escaped
- The implementation follows the same patterns as other validators in the project
- All tests pass and the code follows the project's style guidelines


115 changes: 115 additions & 0 deletions FORK_AND_PUSH_GUIDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Guide: Fork and Push Your Changes

## The Problem
You're trying to push directly to `typestack/class-validator`, but you don't have write permissions to that repository. This is normal for open source projects!

## The Solution: Fork the Repository

### Step 1: Fork the Repository on GitHub

1. Go to https://github.com/typestack/class-validator
2. Click the **"Fork"** button in the top right corner
3. This creates a copy of the repository under your GitHub account (e.g., `https://github.com/YOUR_USERNAME/class-validator`)

### Step 2: Add Your Fork as a Remote

After forking, run these commands (replace `YOUR_USERNAME` with your actual GitHub username):

```bash
# Add your fork as a remote named "fork" (or "myfork")
git remote add fork https://github.com/YOUR_USERNAME/class-validator.git

# Verify remotes
git remote -v
```

You should see:
- `origin` → points to typestack/class-validator (upstream)
- `fork` → points to YOUR_USERNAME/class-validator (your fork)

### Step 3: Stage and Commit Your Changes

```bash
# Stage all changes
git add .

# Commit with the provided message
git commit -F COMMIT_MESSAGE.txt

# Or commit manually
git commit -m "feat: add IsUserName validator for username validation"
```

### Step 4: Push to Your Fork

```bash
# Push to your fork (not origin!)
git push fork feat/add-is-username-validator

# If this is the first push, use:
git push -u fork feat/add-is-username-validator
```

### Step 5: Create a Pull Request

1. Go to your fork on GitHub: `https://github.com/YOUR_USERNAME/class-validator`
2. You'll see a banner saying "feat/add-is-username-validator had recent pushes" with a **"Compare & pull request"** button
3. Click that button
4. Fill in the PR description using the content from `PR_DESCRIPTION.md`
5. Submit the pull request!

## Alternative: If You Already Have a Fork

If you already forked the repository, you might need to update your fork first:

```bash
# Fetch latest changes from upstream
git fetch origin

# Update your local master branch
git checkout master
git pull origin master

# Update your fork
git push fork master
```

## Quick Reference Commands

```bash
# Check current remotes
git remote -v

# Add your fork (one time setup)
git remote add fork https://github.com/YOUR_USERNAME/class-validator.git

# Create feature branch (already done)
git checkout -b feat/add-is-username-validator

# Commit changes
git add .
git commit -F COMMIT_MESSAGE.txt

# Push to your fork
git push -u fork feat/add-is-username-validator
```

## Troubleshooting

### Error: "remote fork already exists"
If you already added your fork, you can update it:
```bash
git remote set-url fork https://github.com/YOUR_USERNAME/class-validator.git
```

### Error: "Permission denied"
Make sure you're pushing to YOUR fork, not the original repository. Use `fork` remote, not `origin`.

### Error: "Updates were rejected"
If your fork is behind, update it first:
```bash
git fetch origin
git rebase origin/master
git push fork feat/add-is-username-validator --force-with-lease
```

102 changes: 102 additions & 0 deletions PR_DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Add IsUserName Validator

## Summary

This PR adds a new `@IsUserName` decorator for validating usernames and human-readable names that may contain special characters like hyphens and apostrophes.

## Features

- ✅ Validates usernames with Unicode support for internationalization
- ✅ Allows letters, numbers, spaces, hyphens (-), and apostrophes (') by default
- ✅ Configurable additional allowed characters via `IsUserNameOptions`
- ✅ Properly escapes special regex characters in custom allowed characters
- ✅ Comprehensive test coverage
- ✅ Usage examples included

## Use Cases

This validator is perfect for validating:
- User full names (e.g., "Mary-Jane O'Brien", "José María")
- Display names
- Human-readable identifiers that may contain hyphens or apostrophes

## Usage Examples

### Basic Usage

```typescript
import { IsUserName } from 'class-validator';

class User {
@IsUserName()
fullName: string; // Valid: "John O'Brien", "Mary-Jane", "José María"
}
```

### With Custom Allowed Characters

```typescript
import { IsUserName } from 'class-validator';

class User {
@IsUserName({ allowedCharacters: '.,' })
displayName: string; // Valid: "Dr. Smith, Jr."
}
```

### With Custom Error Message

```typescript
import { IsUserName } from 'class-validator';

class User {
@IsUserName(
undefined,
{ message: 'Invalid username format' }
)
username: string;
}
```

## Implementation Details

- Uses Unicode property escapes (`\p{L}` for letters, `\p{N}` for numbers) for better internationalization support
- Escapes special regex characters in user-provided `allowedCharacters` to prevent regex injection
- Follows the same patterns as other validators in the project

## Files Changed

### Added
- `src/decorator/string/IsUserName.ts` - Main validator implementation
- `src/decorator/string/IsUserName.spec.ts` - Comprehensive test suite
- `sample/sample10-username-validation/User.ts` - Usage example
- `sample/sample10-username-validation/app.ts` - Example application

### Modified
- `src/decorator/decorators.ts` - Added export for `IsUserName`
- `README.md` - Added documentation to validation decorators table
- `CHANGELOG.md` - Added entry for new feature

## Testing

All tests pass. The test suite includes:
- ✅ Valid usernames with default allowed characters
- ✅ Invalid usernames with special characters
- ✅ Custom allowed characters
- ✅ Edge cases (empty strings, non-string values, unicode characters)
- ✅ Very long names

## Breaking Changes

None. This is a new feature addition.

## Checklist

- [x] Code follows the project's style guidelines
- [x] Tests added/updated and passing
- [x] Documentation updated
- [x] CHANGELOG updated
- [x] No breaking changes
- [x] Examples provided


1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,7 @@ isBoolean(value);
| `@IsUUID(version?: UUIDVersion)` | Checks if the string is a UUID (version 3, 4, 5 or all ). |
| `@IsFirebasePushId()` | Checks if the string is a [Firebase Push ID](https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html) |
| `@IsUppercase()` | Checks if the string is uppercase. |
| `@IsUserName(options?: IsUserNameOptions)` | Checks if the string is a valid username. By default, allows unicode letters, unicode numbers, spaces, hyphens (-), and apostrophes ('). Additional characters can be specified via options. |
| `@Length(min: number, max?: number)` | Checks if the string's length falls in a range. |
| `@MinLength(min: number)` | Checks if the string's length is not less than given number. |
| `@MaxLength(max: number)` | Checks if the string's length is not more than given number. |
Expand Down
Loading