Comments (7)
I think I got a repro
export function withDefault<T extends t.Any>(type: T, defaultValue: t.TypeOf<T>): t.Type<t.InputOf<T>, t.TypeOf<T>> {
return new t.Type(
`withDefault(${type.name}, ${JSON.stringify(defaultValue)})`,
type.is,
(v, c) => type.validate(v != null ? v : defaultValue, c),
type.serialize
)
}
const T1 = t.partial({
name: withDefault(t.string, 'foo')
})
console.log(t.validate({}, T1).fold(() => 'error', a => JSON.stringify(a)))
// => `{}`
Related commit 37c74a5
My opinion is that it's better to give the passed type a chance to perform a deserialization and, in case of failure, fallback to undefinedType
. I'll revert that change.
from io-ts.
This is the version for decoders that I use:
export function withDefault<T extends D.Decoder<unknown, unknown>>(
decoder: T,
defaultValue: D.TypeOf<T>
): D.Decoder<D.InputOf<T>, D.TypeOf<T>> {
return D.union(
decoder,
pipe(
undefinedDecoder,
D.map(() => defaultValue)
)
);
}
export const undefinedDecoder = {
decode: (val: unknown) => {
return val === undefined
? D.success(undefined)
: D.failure(val, "undefined");
},
};
from io-ts.
I think that is already possible
function withDefault<T extends t.Any>(type: T, defaultValue: t.TypeOf<T>): t.Type<t.TypeOf<T>> {
return new t.Type(
type.name,
(v, c) => type.validate(v != null ? v : defaultValue, c)
)
}
from io-ts.
Yeah, that make sense. I would try it as you described in my code and see if it's helpful. If it is, I guess we can discuss making it part of API.
from io-ts.
while this worked for properties in partial types as well until 0.9.1, this does not validate / populate default values anymore in t.partial(s) because of some changes in 0.9.2. this was a breaking change for us :)
ref.
https://github.com/teamdigitale/digital-citizenship-functions/blob/tech-debt/lib/utils/default.ts
https://github.com/teamdigitale/digital-citizenship-functions/blob/tech-debt/lib/utils/__tests__/default.test.ts
from io-ts.
@gunzip links are broken, could you please provide a test case?
from io-ts.
sorry, here are the correct links:
https://github.com/teamdigitale/digital-citizenship-functions/blob/master/lib/utils/default.ts
from io-ts.
Related Issues (20)
- Behavior changes and types are incorrect based on `intersection` array order
- Intersection with record whose keys are a custom type HOT 1
- io-ts recursion use issue HOT 4
- Subpath imports in ESM mode HOT 5
- Clarification question - how to work with the type of codecs themselves? HOT 1
- Surprising acceptance of various inputs HOT 1
- ReadonlyNonEmptyArray can't be used in a mapped type HOT 1
- How to generate documentation for types generated with `t.TypeOf` HOT 1
- Difficulties with generic serialisable type HOT 2
- [Question] Typing a generic mapped union HOT 1
- Inference error for `toString` property in intersection types with TypeScript 4.9.5 HOT 1
- `t.TypeOf<keyof<o>>` should return a string union type, not a numeric union type.
- PSA: TS 5.1 can break using this library HOT 1
- Intersection of Function with Object doesn't validate correctly
- t.Int: use Number.isSafeInteger instead of Number.isInteger
- non-enumerable records with extra keys do not pass `io-ts.record.is`, contrary to TypeScript types HOT 12
- partially enumerable record missing enumerable keys passes `record.is`, contrary to TypeScript types
- Records are stripped when encoding with t.record(NumberFromString, xxx) HOT 2
- Question: Convert a form to a type with different structure
- Incorrect behaviour when used with browser geolocation coordinates object
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 io-ts.