Skip to content

Commit

Permalink
Merge pull request #919 from EltonLobo07/feat/add-values-and-not_valu…
Browse files Browse the repository at this point in the history
…es-actions

add `values` and `notValues` actions
  • Loading branch information
fabian-hiller authored Feb 16, 2025
2 parents df715a2 + 2ff4341 commit 825d6b8
Show file tree
Hide file tree
Showing 47 changed files with 2,430 additions and 7 deletions.
1 change: 1 addition & 0 deletions library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to the library will be documented in this file.
- Add `args` and `returns` action to transform functions (issue #243)
- Add `rfcEmail` action to validate RFC 5322 email addresses (pull request #912)
- Add `gtValue` and `ltValue` action for greater than and less than validation (pull request #978, #985)
- Add `values` and `notValues` action to validate values (pull request #919)
- Add new overload signature to `pipe` and `pipeAync` method to support unlimited pipe items of same input and output type (issue #852)
- Add `@__NO_SIDE_EFFECTS__` notation to improve tree shaking (pull request #995)
- Add `exactOptional` and `exactOptionalAsync` schema (PR #1013)
Expand Down
2 changes: 2 additions & 0 deletions library/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export * from './notGraphemes/index.ts';
export * from './notLength/index.ts';
export * from './notSize/index.ts';
export * from './notValue/index.ts';
export * from './notValues/index.ts';
export * from './notWords/index.ts';
export * from './octal/index.ts';
export * from './partialCheck/index.ts';
Expand Down Expand Up @@ -95,4 +96,5 @@ export * from './ulid/index.ts';
export * from './url/index.ts';
export * from './uuid/index.ts';
export * from './value/index.ts';
export * from './values/index.ts';
export * from './words/index.ts';
17 changes: 10 additions & 7 deletions library/src/actions/notValue/notValue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('notValue', () => {
const nextDate = new Date(+date + 1);
expectNoActionIssue(notValue(date), [
new Date(+date - 1),
new Date(+date + 1),
nextDate,
new Date(+date + 999999),
new Date(nextDate.getTime()),
new Date(nextDate.toISOString()),
Expand All @@ -173,8 +173,7 @@ describe('notValue', () => {
});

test('for valid non-dates', () => {
const date1 = new Date(10);
expectNoActionIssue(notValue(date1), [
expectNoActionIssue(notValue(new Date(10)), [
9n,
11n,
9,
Expand All @@ -190,8 +189,7 @@ describe('notValue', () => {
true,
false,
]);
const date2 = new Date(1);
expectNoActionIssue(notValue(date2), [
expectNoActionIssue(notValue(new Date(1)), [
0,
0.0,
0n,
Expand All @@ -202,8 +200,7 @@ describe('notValue', () => {
' ',
false,
]);
const date3 = new Date(0);
expectNoActionIssue(notValue(date3), [
expectNoActionIssue(notValue(new Date(0)), [
1,
1.0,
1n,
Expand Down Expand Up @@ -448,6 +445,12 @@ describe('notValue', () => {
});

test('for invalid non-strings', () => {
expectActionIssue(
notValue('123', 'message'),
{ ...baseInfo, expected: '!"123"', requirement: '123' },
[123, 123.0, 123n, new Date(123)],
getReceived
);
expectActionIssue(
notValue('1', 'message'),
{ ...baseInfo, expected: '!"1"', requirement: '1' },
Expand Down
1 change: 1 addition & 0 deletions library/src/actions/notValues/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './notValues.ts';
49 changes: 49 additions & 0 deletions library/src/actions/notValues/notValues.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { describe, expectTypeOf, test } from 'vitest';
import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts';
import {
notValues,
type NotValuesAction,
type NotValuesIssue,
} from './notValues.ts';

describe('notValues', () => {
describe('should return action object', () => {
test('with undefined message', () => {
type Action = NotValuesAction<number, [7, 12], undefined>;
expectTypeOf(notValues<number, [7, 12]>([7, 12])).toEqualTypeOf<Action>();
expectTypeOf(
notValues<number, [7, 12], undefined>([7, 12], undefined)
).toEqualTypeOf<Action>();
});

test('with string message', () => {
expectTypeOf(
notValues<number, [7, 12], 'message'>([7, 12], 'message')
).toEqualTypeOf<NotValuesAction<number, [7, 12], 'message'>>();
});

test('with function message', () => {
expectTypeOf(
notValues<number, [7, 12], () => string>([7, 12], () => 'message')
).toEqualTypeOf<NotValuesAction<number, [7, 12], () => string>>();
});
});

describe('should infer correct types', () => {
type Action = NotValuesAction<number, [7, 12], undefined>;

test('of input', () => {
expectTypeOf<InferInput<Action>>().toEqualTypeOf<number>();
});

test('of output', () => {
expectTypeOf<InferOutput<Action>>().toEqualTypeOf<number>();
});

test('of issue', () => {
expectTypeOf<InferIssue<Action>>().toEqualTypeOf<
NotValuesIssue<number, [7, 12]>
>();
});
});
});
Loading

0 comments on commit 825d6b8

Please sign in to comment.