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
14 changes: 8 additions & 6 deletions library/src/methods/cache/cache.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { OutputDataset } from '../../types/index.ts';

Check warning on line 1 in library/src/methods/cache/cache.ts

View workflow job for this annotation

GitHub Actions / Run ESLint in library

'/home/runner/work/valibot/valibot/library/src/types/index.ts' imported multiple times
import type {
BaseIssue,
BaseSchema,
InferIssue,
InferOutput,
} from '../../types/index.ts';

Check warning on line 7 in library/src/methods/cache/cache.ts

View workflow job for this annotation

GitHub Actions / Run ESLint in library

'/home/runner/work/valibot/valibot/library/src/types/index.ts' imported multiple times
import { _cloneDataset, _getStandardProps } from '../../utils/index.ts';
import { _cloneDataset, _standardSchema } from '../../utils/index.ts';
import { _LruCache } from './_LruCache.ts';
import type { Cache, CacheConfig } from './types.ts';

Expand Down Expand Up @@ -79,13 +79,15 @@
BaseSchema<unknown, unknown, BaseIssue<unknown>>,
CacheConfig | undefined
> {
return {
return _standardSchema<
SchemaWithCache<
BaseSchema<unknown, unknown, BaseIssue<unknown>>,
CacheConfig | undefined
>
>({
...schema,
cacheConfig: config,
cache: new _LruCache(config),
get '~standard'() {
return _getStandardProps(this);
},
'~run'(dataset, runConfig) {
const key = this.cache.key(dataset.value, runConfig);
let outputDataset = this.cache.get(key);
Expand All @@ -99,5 +101,5 @@
// do not mutate the cached dataset wrapper or issues array.
return _cloneDataset(outputDataset);
},
};
});
}
15 changes: 9 additions & 6 deletions library/src/methods/cache/cacheAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
StandardProps,
UnknownDataset,
} from '../../types/index.ts';
import { _cloneDataset, _getStandardProps } from '../../utils/index.ts';
import { _cloneDataset, _standardSchema } from '../../utils/index.ts';
import { _LruCache } from './_LruCache.ts';
import type { Cache, CacheConfig } from './types.ts';

Expand Down Expand Up @@ -128,14 +128,17 @@ export function cacheAsync(
let activeRuns:
| Map<string, Promise<OutputDataset<unknown, BaseIssue<unknown>>>>
| undefined;
return {
return _standardSchema<
SchemaWithCacheAsync<
| BaseSchema<unknown, unknown, BaseIssue<unknown>>
| BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>,
CacheConfig | undefined
>
>({
...schema,
async: true,
cacheConfig: config,
cache: new _LruCache(config),
get '~standard'() {
return _getStandardProps(this);
},
async '~run'(dataset, runConfig) {
// Create cache key based on input and config
const key = this.cache.key(dataset.value, runConfig);
Expand Down Expand Up @@ -169,5 +172,5 @@ export function cacheAsync(
activeRuns?.delete(key);
}
},
};
});
}
9 changes: 3 additions & 6 deletions library/src/methods/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
Config,
InferIssue,
} from '../../types/index.ts';
import { _getStandardProps } from '../../utils/index.ts';
import { _standardSchema } from '../../utils/index.ts';

/**
* Changes the local configuration of a schema.
Expand All @@ -21,13 +21,10 @@ export function config<
| BaseSchema<unknown, unknown, BaseIssue<unknown>>
| BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>,
>(schema: TSchema, config: Config<InferIssue<TSchema>>): TSchema {
return {
return _standardSchema<TSchema>({
...schema,
get '~standard'() {
return _getStandardProps(this);
},
'~run'(dataset, config_) {
return schema['~run'](dataset, { ...config_, ...config });
},
};
});
}
11 changes: 4 additions & 7 deletions library/src/methods/fallback/fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
MaybeDeepReadonly,
OutputDataset,
} from '../../types/index.ts';
import { _getStandardProps } from '../../utils/index.ts';
import { _standardSchema } from '../../utils/index.ts';
import { getFallback } from '../getFallback/index.ts';

/**
Expand Down Expand Up @@ -51,17 +51,14 @@ export function fallback<
schema: TSchema,
fallback: TFallback
): SchemaWithFallback<TSchema, TFallback> {
return {
return _standardSchema<SchemaWithFallback<TSchema, TFallback>>({
...schema,
fallback,
get '~standard'() {
return _getStandardProps(this);
},
'~run'(dataset, config) {
'~run'(this: SchemaWithFallback<TSchema, TFallback>, dataset, config) {
const outputDataset = schema['~run'](dataset, config);
return outputDataset.issues
? { typed: true, value: getFallback(this, outputDataset, config) }
: outputDataset;
},
};
});
}
23 changes: 15 additions & 8 deletions library/src/methods/fallback/fallbackAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
StandardProps,
UnknownDataset,
} from '../../types/index.ts';
import { _getStandardProps } from '../../utils/index.ts';
import { _standardSchema } from '../../utils/index.ts';
import { getFallback } from '../getFallback/index.ts';

/**
Expand Down Expand Up @@ -89,21 +89,28 @@ export function fallbackAsync<
schema: TSchema,
fallback: TFallback
): SchemaWithFallbackAsync<TSchema, TFallback> {
return {
return _standardSchema<SchemaWithFallbackAsync<TSchema, TFallback>>({
...schema,
fallback,
async: true,
get '~standard'() {
return _getStandardProps(this);
},
async '~run'(dataset, config) {
async '~run'(
dataset: UnknownDataset,
config: Config<BaseIssue<unknown>>
): Promise<OutputDataset<InferOutput<TSchema>, InferIssue<TSchema>>> {
const outputDataset = await schema['~run'](dataset, config);
return outputDataset.issues
? {
typed: true,
value: await getFallback(this, outputDataset, config),
value: await getFallback(
this as SchemaWithFallbackAsync<TSchema, TFallback>,
outputDataset,
config
),
}
: outputDataset;
},
};
} as unknown as Omit<
SchemaWithFallbackAsync<TSchema, TFallback>,
'~standard'
>);
Comment on lines +112 to +115
}
9 changes: 3 additions & 6 deletions library/src/methods/message/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
ErrorMessage,
InferIssue,
} from '../../types/index.ts';
import { _getStandardProps } from '../../utils/index.ts';
import { _standardSchema } from '../../utils/index.ts';

/**
* Changes the local message configuration of a schema.
Expand All @@ -21,13 +21,10 @@ export function message<
| BaseSchema<unknown, unknown, BaseIssue<unknown>>
| BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>,
>(schema: TSchema, message_: ErrorMessage<InferIssue<TSchema>>): TSchema {
return {
return _standardSchema<TSchema>({
...schema,
get '~standard'() {
return _getStandardProps(this);
},
'~run'(dataset, config) {
return schema['~run'](dataset, { ...config, message: message_ });
},
};
});
}
9 changes: 9 additions & 0 deletions library/src/methods/omit/omit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ describe('omit', () => {
} satisfies FailureDataset<InferIssue<typeof schema>>);
});
});

test('should validate against the omitted entries via "~standard"', () => {
expect(
schema['~standard'].validate({ key2: 123, key4: 456 })
).toMatchObject({ value: { key2: 123, key4: 456 } });
expect(
schema['~standard'].validate({ key1: 'foo', key2: 123, key4: 456 })
).toMatchObject({ value: { key2: 123, key4: 456 } });
});
});

describe('objectWithRest', () => {
Expand Down
9 changes: 3 additions & 6 deletions library/src/methods/omit/omit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type {
StandardProps,
UnknownDataset,
} from '../../types/index.ts';
import { _getStandardProps } from '../../utils/index.ts';
import { _standardSchema } from '../../utils/index.ts';

/**
* Schema type.
Expand Down Expand Up @@ -481,11 +481,8 @@ export function omit<

// Rerturn modified copy of schema
// @ts-expect-error
return {
return _standardSchema<SchemaWithOmit<TSchema, TKeys>>({
...schema,
entries,
get '~standard'() {
return _getStandardProps(this);
},
};
});
}
9 changes: 9 additions & 0 deletions library/src/methods/partial/partial.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ describe('partial', () => {
}
});
});

test('should validate against the partialed entries via "~standard"', () => {
expect(schema2['~standard'].validate({ key2: 123 })).toMatchObject({
value: { key2: 123, key4: 123 },
});
expect(schema2['~standard'].validate({})).toMatchObject({
issues: [{ path: [{ key: 'key2' }] }],
});
});
});

describe('objectWithRest', () => {
Expand Down
11 changes: 5 additions & 6 deletions library/src/methods/partial/partial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type {
StandardProps,
UnknownDataset,
} from '../../types/index.ts';
import { _getStandardProps } from '../../utils/index.ts';
import { _standardSchema } from '../../utils/index.ts';

/**
* Schema type.
Expand Down Expand Up @@ -282,11 +282,10 @@ export function partial(
}

// Return modified copy of schema
return {
return _standardSchema<
SchemaWithPartial<Schema, ObjectKeys<Schema> | undefined>
>({
...schema,
entries,
get '~standard'() {
return _getStandardProps(this);
},
};
});
}
11 changes: 5 additions & 6 deletions library/src/methods/partial/partialAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import type {
StandardProps,
UnknownDataset,
} from '../../types/index.ts';
import { _getStandardProps } from '../../utils/index.ts';
import { _standardSchema } from '../../utils/index.ts';

/**
* Schema type.
Expand Down Expand Up @@ -293,11 +293,10 @@ export function partialAsync(
}

// Return modified copy of schema
return {
return _standardSchema<
SchemaWithPartialAsync<Schema, ObjectKeys<Schema> | undefined>
>({
...schema,
entries,
get '~standard'() {
return _getStandardProps(this);
},
};
});
}
9 changes: 3 additions & 6 deletions library/src/methods/pick/pick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type {
StandardProps,
UnknownDataset,
} from '../../types/index.ts';
import { _getStandardProps } from '../../utils/index.ts';
import { _standardSchema } from '../../utils/index.ts';

/**
* The schema type.
Expand Down Expand Up @@ -478,11 +478,8 @@ export function pick<

// Return modified copy of schema
// @ts-expect-error
return {
return _standardSchema<SchemaWithPick<TSchema, TKeys>>({
...schema,
entries,
get '~standard'() {
return _getStandardProps(this);
},
};
});
}
25 changes: 25 additions & 0 deletions library/src/methods/pipe/pipe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
decimal,
type DecimalIssue,
description,
maxLength,
minLength,
type MinLengthIssue,
minValue,
Expand Down Expand Up @@ -151,4 +152,28 @@ describe('pipe', () => {
});
});
});

describe('should have correct "~standard" props', () => {
test('should validate against the piped schema, not the source', () => {
const piped = pipe(string(), maxLength(3));
expect(piped['~standard'].validate('too long')).toMatchObject({
issues: [{ message: 'Invalid length: Expected <=3 but received 8' }],
});
expect(piped['~standard'].validate('ab')).toMatchObject({
value: 'ab',
});
});

test('should return same object on repeated access', () => {
expect(schema['~standard']).toBe(schema['~standard']);
});

test('should work when destructured', () => {
const piped = pipe(string(), maxLength(3));
const { validate } = piped['~standard'];
expect(validate('too long')).toMatchObject({
issues: [{ message: 'Invalid length: Expected <=3 but received 8' }],
});
});
});
});
9 changes: 3 additions & 6 deletions library/src/methods/pipe/pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
StandardProps,
UnknownDataset,
} from '../../types/index.ts';
import { _getStandardProps } from '../../utils/index.ts';
import { _standardSchema } from '../../utils/index.ts';

/**
* Schema with pipe type.
Expand Down Expand Up @@ -2692,12 +2692,9 @@ export function pipe<
>(
...pipe: [TSchema, ...TItems]
): SchemaWithPipe<readonly [TSchema, ...TItems]> {
return {
return _standardSchema<SchemaWithPipe<readonly [TSchema, ...TItems]>>({
...pipe[0],
pipe,
get '~standard'() {
return _getStandardProps(this);
},
'~run'(dataset, config) {
// Execute pipeline items in sequence
for (const item of pipe) {
Expand Down Expand Up @@ -2728,5 +2725,5 @@ export function pipe<
// @ts-expect-error
return dataset as OutputDataset<unknown, BaseIssue<unknown>>;
},
};
});
}
Loading
Loading