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

feat: add match regex validation #301

Closed
Closed
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
1 change: 1 addition & 0 deletions library/src/validations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export * from './length/index.ts';
export * from './mac/index.ts';
export * from './mac48/index.ts';
export * from './mac64/index.ts';
export * from './match/index.ts';
export * from './maxBytes/index.ts';
export * from './maxLength/index.ts';
export * from './maxSize/index.ts';
Expand Down
1 change: 1 addition & 0 deletions library/src/validations/match/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './match.ts';
37 changes: 37 additions & 0 deletions library/src/validations/match/match.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, test } from 'vitest';
import { match } from './match.ts';

describe('match', () => {
test('should pass only valid matches', () => {
const validate = match(/abc/u);
const value1 = 'abc';
expect(validate._parse(value1).output).toBe(value1);
const value2 = 'abcdev';
expect(validate._parse(value2).output).toBe(value2);
const value3 = '123abc';
expect(validate._parse(value3).output).toBe(value3);

const validate2 = match(/abc/iu);
const value4 = 'abc';
expect(validate2._parse(value4).output).toBe(value4);
const value5 = 'abcdef';
expect(validate2._parse(value5).output).toBe(value5);
const value6 = '123abc';
expect(validate2._parse(value6).output).toBe(value6);
const value7 = 'AbCd%';
expect(validate2._parse(value7).output).toBe(value7);

expect(validate._parse('').issues).toBeTruthy();
expect(validate._parse('acb').issues).toBeTruthy();
expect(validate._parse('Abc').issues).toBeTruthy();
expect(validate._parse('123').issues).toBeTruthy();

expect(validate2._parse('acb').issues).toBeTruthy();
});

test('should return custom error message', () => {
const error = 'Value not matching digit numbers!';
const validate = match(/^\d/u, error);
expect(validate._parse('abc').issues?.[0].message).toBe(error);
});
});
44 changes: 44 additions & 0 deletions library/src/validations/match/match.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { BaseValidation, ErrorMessage } from '../../types/index.ts';
import { actionIssue, actionOutput } from '../../utils/index.ts';

/**
* Match validation type.
*/
export type MatchValidation<
TInput extends string,
TRequirement extends RegExp
> = BaseValidation<TInput> & {
/**
* The validation type.
*/
type: 'match';
/**
* The regular expression requirement
*/
requirement: TRequirement;
};

/**
* Creates a validation function that validates string against regular expression [RegExp](https://en.wikipedia.org/wiki/Regular_expression).
*
* @param requirement regular expression requirement
* @param message The error message.
*
* @returns A validation function.
*/
export function match<TInput extends string, TRequirement extends RegExp>(
requirement: TRequirement,
message: ErrorMessage = 'Invalid string'
): MatchValidation<TInput, TRequirement> {
return {
type: 'match',
async: false,
message,
requirement,
_parse(input) {
return !this.requirement.test(input)
? actionIssue(this.type, this.message, input, this.requirement)
: actionOutput(input);
},
};
}
9 changes: 9 additions & 0 deletions website/src/routes/api/(validations)/match/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: match
contributors:
- ariskemper
---

# match

> The content of this page is not yet ready. Until then just use the [source code](https://github.com/fabian-hiller/valibot/blob/main/library/src/validations/match/match.ts).
1 change: 1 addition & 0 deletions website/src/routes/api/menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
- [mac](/api/mac/)
- [mac48](/api/mac48/)
- [mac64](/api/mac64/)
- [match](/api/match/)
- [maxBytes](/api/maxBytes/)
- [maxLength](/api/maxLength/)
- [maxSize](/api/maxSize/)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Validation functions examine the input and, if the input does not meet a certain
'mac',
'mac48',
'mac64',
'match',
'maxBytes',
'maxLength',
'maxSize',
Expand Down
Loading