Comments (16)
The way it was resolved in kysely:
from valibot.
It should be related to :
from valibot.
Thank you for the tip. I will try to investigate this in the next few weeks.
from valibot.
Can you send me some code examples?
from valibot.
Yes, that works too. Thank you! Another workaround that works in most cases is intersection
:
import { intersection, object, string } from 'valibot';
const Object1 = object({ key1: string() });
const Object2 = object({ key2: string() });
const Object3 = intersection([Object1, Object2]);
from valibot.
Here is a guide that help to choose between intersect
and merge
: https://valibot.dev/guides/intersections/
from valibot.
I am toying with the idea of removing merge
because of this TypeScript problem. Meanwhile, Valibot has a workaround that accomplishes exactly the same thing with a good DX. Please give me feedback.
import * as v from 'valibot';
const ObjectSchema1 = v.object({ key1: v.string() });
const ObjectSchema2 = v.object({ key2: v.number() });
const MergedSchema1 = v.merge([ObjectSchema1, ObjectSchema2]);
// This is the workaround that accomplishes exactly the same thing
const MergedSchema2 = v.object({
...ObjectSchema1.entries,
...ObjectSchema2.entries,
});
from valibot.
I have created a repo with my code.
For context, it's a bunch of schemas for the Notion API as well as a fetch wrapper.
My intent with this library is to validate the API responses and use schema transformations to simplify the structure sent by Notion.
- The schemas/inputs contains the schemas I constructed and refactored from the API results (Notion API has a lot of union types).
- The schemas contains the transformations.
- The problem occurs in page
- If I comment
properties
which is arecord
of anunion
of many schemas, the problem disappears :
export const vIPage = merge([
vIPageOrDatabaseCommon,
object({
object: literal('page'),
parent: union([vIParentDatabase, vIParentPage, vIParentWorkspace]),
// properties: record(vIPageProp),
}),
]);
from valibot.
Yes, the problem is vIPageProp
which is used with properties: record(vIPageProp)
in vIPage
. However, I have not found out the exact cause yet. I will investigate this further.
from valibot.
I was able to reproduce the problem. However, I could not find the bug yet. I will investigate further in a moment. It is interesting to note that the problem does not occur in TypeScript v4.9.5. Therefore, it could also be a bug in TypeScript v5.
import { merge, omit, object, union, transform, record, string } from "valibot";
const Merge1 = merge([object({}), object({ key: string() })]);
const Merge2 = merge([Merge1, object({})]);
const Object1 = merge([Merge2, object({})]);
const Object2 = merge([omit(Merge2, ["key"]), object({})]);
const Record = record(union([Object1, Object2]));
transform(Record, (a) => a); // Error: Type instantiation is excessively deep and possibly infinite.ts(2589)
from valibot.
So as a workaround, you could downgrade TypeScript. It is important that you set the TypeScript version in VS Code to the version in your package.json
. If you don't know how to do that and can't figure it out yourself, I can explain it to you here.
from valibot.
I have thoroughly checked the source code of omit
and merge
and cannot find any problem. I also noticed that when I copy the source code of merge
into the project and update the imports, the problem disappears, even though it is the same code.
Since I have similar problems with Modular Forms using TypeScript v5, I assume that it must be a bug in TypeScript. I can't explain it any other way at the moment. If anyone else wants to investigate the issue further, I'm happy to hear the results. Until then, for all other users, I recommend downgrading TypeScript to v4.9.5.
from valibot.
Thank you for your reactivity! Yes, I'll try to downgrade and see if everything works smoothly :) At the same time, I'll investigate about that problem too.
from valibot.
I'm getting the same error message when using with object
+ merge
.
As a workaround I'm using ObjectSchema.object
property, like this:
import {object} from "valibot"
import {FileOutput} from "./FileOutput.js"
import {ImageSize} from "./ImageSize.js"
export const ImageOutput = object({
...FileOutput.object,
metadata: ImageSize
})
from valibot.
I noticed marge slowdown in my TypeScript checking performance recently. I tracked it back to this and a switch from merge
to intersect
(the name has changed since this issue was created) cut my type checking time in half.
Running TypeScript v5.3.3 and Valibot v0.25.0.
from valibot.
I think this is fixed in v0.30.0
from valibot.
Related Issues (20)
- Request for Enhancement: Support for Multiple Keys in variant Function HOT 3
- Derived Schema Validations? HOT 1
- Partial variant schema HOT 1
- Can't get `instance` to work HOT 1
- [i18n] equal is still present in translations HOT 1
- Playground UI/UX HOT 1
- Cannot use `variant` together with `merge` HOT 1
- Union of literals HOT 1
- Add passthrough object schema HOT 3
- Omit removes rest object props HOT 12
- Ability to extend existing declared schemas without redefining HOT 4
- TS error when using custom() HOT 9
- How do I derive a schema with more validations/transformations? HOT 1
- Allow errorMap (like zod) message for literal HOT 11
- `intersect` of `object` and `record` does not work like Typescript HOT 9
- using `union` as key of `record` adds `undefined` to the value type HOT 13
- `isoTimestamp` only accepts UTC timestamps HOT 3
- Not able to validate file uploads on the backend, even though it works on the frontend HOT 4
- Search fails to work HOT 1
- Timestamp regex doesn't support timezone other than Zulu HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from valibot.