Skip to content

Commit

Permalink
Merge pull request #309 from ariskemper/feat-octal-validation
Browse files Browse the repository at this point in the history
feat: add octal validation
  • Loading branch information
fabian-hiller authored Dec 23, 2023
2 parents e779e31 + dcb6e25 commit d378d20
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to the library will be documented in this file.

## vX.X.X (Month DD, YYYY)

- Add `creditCard`, `hash`, `hex` and `hexColor` validation function (pull request #292, #304, #307, #308)
- Add `creditCard`, `hash`, `hex`, `hexColor` and `octal` validation function (pull request #292, #304, #307, #308, #309)

## v0.24.1 (December 11, 2023)

Expand Down
5 changes: 5 additions & 0 deletions library/src/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ export const MAC48_REGEX =
export const MAC64_REGEX =
/^(?:[\da-f]{2}:){7}[\da-f]{2}$|^(?:[\da-f]{2}-){7}[\da-f]{2}$|^(?:[\da-f]{4}\.){3}[\da-f]{4}$|^(?:[\da-f]{4}:){3}[\da-f]{4}$/iu;

/**
* [Octal](https://en.wikipedia.org/wiki/Octal) regex.
*/
export const OCTAL_REGEX = /^(0o)?[0-7]+$/iu;

/**
* [ULID](https://github.com/ulid/spec) regex.
*/
Expand Down
1 change: 1 addition & 0 deletions library/src/validations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export * from './notBytes/index.ts';
export * from './notLength/index.ts';
export * from './notSize/index.ts';
export * from './notValue/index.ts';
export * from './octal/index.ts';
export * from './regex/index.ts';
export * from './safeInteger/index.ts';
export * from './size/index.ts';
Expand Down
1 change: 1 addition & 0 deletions library/src/validations/octal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './octal.ts';
47 changes: 47 additions & 0 deletions library/src/validations/octal/octal.test.ts
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);
});
});
40 changes: 40 additions & 0 deletions library/src/validations/octal/octal.ts
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);
},
};
}
1 change: 1 addition & 0 deletions website/src/routes/api/(schemas)/any/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ The following APIs can be combined with `any`.
'notLength',
'notSize',
'notValue',
'octal',
'regex',
'safeInteger',
'size',
Expand Down
1 change: 1 addition & 0 deletions website/src/routes/api/(schemas)/string/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ The following APIs can be combined with `string`.
'notBytes',
'notLength',
'notValue',
'octal',
'regex',
'startsWith',
'ulid',
Expand Down
9 changes: 9 additions & 0 deletions website/src/routes/api/(validations)/octal/index.mdx
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).
1 change: 1 addition & 0 deletions website/src/routes/api/menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
- [notLength](/api/notLength/)
- [notSize](/api/notSize/)
- [notValue](/api/notValue/)
- [octal](/api/octal/)
- [regex](/api/regex/)
- [safeInteger](/api/safeInteger/)
- [size](/api/size/)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Validation functions examine the input and, if the input does not meet a certain
'notLength',
'notSize',
'notValue',
'octal',
'regex',
'safeInteger',
'size',
Expand Down

0 comments on commit d378d20

Please sign in to comment.