-
-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #309 from ariskemper/feat-octal-validation
feat: add octal validation
- Loading branch information
Showing
11 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './octal.ts'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { octal } from './octal.ts'; | ||
|
||
describe('octal', () => { | ||
test('should pass only valid strings', () => { | ||
const validate = octal(); | ||
|
||
const value = '123'; | ||
expect(validate._parse(value).output).toBe(value); | ||
const value2 = '001'; | ||
expect(validate._parse(value2).output).toBe(value2); | ||
const value3 = '765'; | ||
expect(validate._parse(value3).output).toBe(value3); | ||
const value4 = '000'; | ||
expect(validate._parse(value4).output).toBe(value4); | ||
const value5 = '111'; | ||
expect(validate._parse(value5).output).toBe(value5); | ||
const value6 = '020'; | ||
expect(validate._parse(value6).output).toBe(value6); | ||
const value7 = '707'; | ||
expect(validate._parse(value7).output).toBe(value7); | ||
const value8 = '00012345'; | ||
expect(validate._parse(value8).output).toBe(value8); | ||
const value9 = '0o12345'; | ||
expect(validate._parse(value9).output).toBe(value9); | ||
|
||
expect(validate._parse('').issues).toBeTruthy(); | ||
expect(validate._parse('8').issues).toBeTruthy(); | ||
expect(validate._parse('9').issues).toBeTruthy(); | ||
expect(validate._parse('789').issues).toBeTruthy(); | ||
expect(validate._parse('1238').issues).toBeTruthy(); | ||
expect(validate._parse('abc').issues).toBeTruthy(); | ||
expect(validate._parse('0123456789').issues).toBeTruthy(); | ||
expect(validate._parse('0078').issues).toBeTruthy(); | ||
expect(validate._parse('056A').issues).toBeTruthy(); | ||
expect(validate._parse('99').issues).toBeTruthy(); | ||
expect(validate._parse('08').issues).toBeTruthy(); | ||
expect(validate._parse('%123').issues).toBeTruthy(); | ||
expect(validate._parse('00o123').issues).toBeTruthy(); | ||
}); | ||
|
||
test('should return custom error message', () => { | ||
const error = 'Value does not match the octal!'; | ||
const validate = octal(error); | ||
expect(validate._parse('test').issues?.[0].message).toBe(error); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { OCTAL_REGEX } from '../../regex.ts'; | ||
import type { BaseValidation, ErrorMessage } from '../../types/index.ts'; | ||
import { actionIssue, actionOutput } from '../../utils/index.ts'; | ||
|
||
/** | ||
* Octal validation type. | ||
*/ | ||
export type OctalValidation<TInput extends string> = BaseValidation<TInput> & { | ||
/** | ||
* The validation type. | ||
*/ | ||
type: 'octal'; | ||
/** | ||
* The octal regex. | ||
*/ | ||
requirement: RegExp; | ||
}; | ||
|
||
/** | ||
* Creates a validation function that validates an [octal](https://en.wikipedia.org/wiki/Octal) string. | ||
* | ||
* @param message The error message. | ||
* | ||
* @returns A validation function. | ||
*/ | ||
export function octal<TInput extends string>( | ||
message: ErrorMessage = 'Invalid octal' | ||
): OctalValidation<TInput> { | ||
return { | ||
type: 'octal', | ||
async: false, | ||
message, | ||
requirement: OCTAL_REGEX, | ||
_parse(input) { | ||
return !this.requirement.test(input) | ||
? actionIssue(this.type, this.message, input, this.requirement) | ||
: actionOutput(input); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
title: octal | ||
contributors: | ||
- ariskemper | ||
--- | ||
|
||
# octal | ||
|
||
> 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/octal/octal.ts). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters