Comments (3)
Actually I think there a more functional approach that you'd probably recommend.
We just add a strictObject(decoders)
function that invokes object(decoders)
.
Here's my untested JavaScript (not TypeScript) version
const checkStrict = (decoders) => {
return new Decoder((json) => {
for (const key in json) {
if (!decoders.hasOwnProperty(key)) {
return Result.err({message: `an undefined key '${key}' is present in the object`});
}
}
return Result.ok(json);
});
}
// like object() but checks that only declared fields are used.
export const strictObject = (decoders) => object(decoders).andThen(checkStrict(decoders));
from json-type-validation.
Alas .. That won't work because you've made the Decoder() constructor private.
from json-type-validation.
I'm also looking for such a feature. In keeping with the library perhaps there could be an only
combinator, so a decoder could be defined as:
const decoder = only(object({a: string()})
where this decoder would throw on {a: "hello", b: "world"} but not {a: "hello world"}.
from json-type-validation.
Related Issues (20)
- Support custom field name for validation HOT 9
- Enum decoders are cumbersome and not intuitive HOT 6
- Should decoders match their decoded type exactly, or should there be multiple valid decoders for a type? HOT 11
- andThen decoder should return intersection type HOT 2
- Support `nullable` type. HOT 3
- Provide script that can rewrite TS interfaces to object({}) HOT 3
- Possible to represent fixed-length arrays / "tuples"? HOT 3
- Decode index type objects? HOT 2
- Release recent changes? HOT 2
- Feature: 'nullToUndefined' decoder type HOT 3
- Recursion not working HOT 3
- Feature: dict with number keys HOT 2
- 'runWithException' should throw instance of Error HOT 1
- object() compiles to Object() in Babel which breaks the lib HOT 1
- Possible to validate hash table of arbitrary length?
- Lodash Dependency HOT 3
- TypeScript compilation error
- object decoder should have an option to throw error if extra properties are present
- Generate OpenAPI schema objects from decoders
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 json-type-validation.