Comments (2)
I'm still chewing this over, but here are some initial thoughts
I think the best way to think about the scope of this project is that decoders take in values of the type returned by JSON.parse
and validate to more specific types. Unfortunately the return type of JSON.parse
is any
. It would be nice if we instead got a more accurate JsonValue
type to reflect that functions etc are not included. Maybe it would have been better to support any JS value, but I don't think I'm going to go back on that at this point. Different goals, different project.
So while it's true that the json spec requires object fields to be strings, when we parse a json encoded object into javascript the resulting JS object can have both string and number keys. So objects with number keys are part of the JsonValue
type.
Maybe relevant, here's how typescript handles string vs number records:
const c: Record<string, string> = {1: "a"}
c["1"] // just fine
c[1] // also fine
vs
const c: Record<number, string> = {1: "a"}
c["1"] // error
c[1] // ok
@laurence-bird have you experimented with finding a way to get the behavior you want with the existing decoders? I ask because I'm not 100% sure if it is or is not possible to create a Decoder<Record<number, A>>
already. That would be my first step before evaluating the best way to add a new feature.
Thanks for the question. I'd look over a PR, but at this point I'm not sure exactly what the API would look like, so I'm not yet confident if this is a feature we can or want to support.
from json-type-validation.
@mulias there's an implied expectation that input is valid JSON ("json-type-validation
"), but it doesn't technically have to be, right? I can't remember if you explicitly made a determination on that.
Otherwise, JSON object fields must be string
s:
member
ws string ws ':' element
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
- 'runWithException' should throw instance of Error HOT 1
- object() decoder needs a 'strict' option to disallow undefined fields HOT 3
- 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.