Comments (2)
I think this can be achieved with Valita's builtin features by using the strict parsing mode and inferring the Parent
and Child
types from the final parser:
const Parent = v.object({
children: v
.record(
v.object({
color: v.string(),
})
)
.map((children) =>
Object.fromEntries(
Object.entries(children).map(([name, child]) => [
name,
{ name, ...child },
])
)
),
});
type Parent = v.Infer<typeof Parent>;
type Child = Parent["children"][string];
const example = Parent.parse(someInput, { mode: "strict" });
The strict mode makes Valita fail parsing when it encounters an unexpected object key:
Parent.parse(
{
children: { alice: { color: "blue", name: "bob" } },
},
{ mode: "strict" }
);
// ValitaError: unrecognized_key at .children.alice (unrecognized key "name")
In fact, since Valita v0.0.21 released yesterday, the strict mode is the default (the old behavior can be enabled by setting { mode: "passthrough" }
):
Parent.parse({
children: { alice: { color: "blue", name: "bob" } },
});
// ValitaError: unrecognized_key at .children.alice (unrecognized key "name")
It's also possible for v.object
to "opt-in" to strict parsing regardless of the parsing mode:
// fail parsing if "name" is present.
v.object({
color: v.string(),
name: v.never().optional()
})
or
// fail parsing if "color" is not the only key present
v.object({
color: v.string(),
})
.rest(v.never())
from valita.
I'm assuming this was solved, so closing this issue 👍
from valita.
Related Issues (20)
- v.lazy() causes error HOT 1
- Validating typed arrays HOT 5
- Deno support HOT 1
- Exporting AbstractType HOT 2
- A safe way to transform Zod parser to Valita HOT 2
- Input & output types HOT 10
- TS 4094, issueTrue is private HOT 2
- Can t.Infer be made to _not_ unroll deep schemas? HOT 4
- Annotation support? HOT 5
- Runtime schema inspection / type narrowing HOT 5
- RFE: Add a way to infer readonly types HOT 2
- Improved error reporting for .map() HOT 2
- Help developer catch error early HOT 6
- Should we add `intersect` method to UnionType? HOT 2
- export type classes HOT 6
- Why I can't use `.try(value)` when using `.optional()`? HOT 3
- What is the purpose of `.default(...)` ? HOT 2
- The `protoless` optimization breaks Firebase/Firestore HOT 1
- Passing ParseOptions to chain? HOT 6
- Question: How to combine records and objects HOT 2
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 valita.