Releases: Nemikolh/typesafe-schema
Typesafe-schema v0.5.0
Add support for the new TypeScript compiler option exactOptionalPropertyTypes
.
This adds a new construct: ObjWithOptional
.
ObjithOptional
mark properties from its second argument as optional in the TypeScript sense.
For each prop defined in the second argument, you get prop?: type
for them
instead of prop: type | undefined
or prop: type
.
To avoid confusion, in this release Optional
is renamed to MaybeUndefined
.
Here is an example of the new ObjWithOptional
:
ObjWithOptional({
a: NUMBER,
}, {
b: STRING
})
// This schema will typecheck values as:
{
a: number,
b?: string
}
// If you want b to possibly be undefined you can do instead:
ObjWithOptional({
a: NUMBER,
}, {
b: MaybeUndefined(STRING)
})
// This will typecheck values as:
{
a: number,
b?: string | undefined
}
Note that this only works when the new flag is set to true. If the flag is set to false or if you use an older version of TypeScript then in both case the type will be:
{
a: number,
b?: string | undefined
}
typesafe-schema
does not look whether the flag is set to true or not. This means that even though the compiler won't differentiate between the two cases this library will.
Typesafe-schema v0.4.9
Add MinLength
a helper to restrict values to be of a certain length. Length has a different meaning depending on the context.
For array and string, the length
property is checked. For dictionaries (Dict
), the length is computed with Object.keys(value).length
.
Support up to 8 values in type generated by Enum
With this release, the following will be valid:
const a = newValidator(Enum('a', 'b', 'c', 'd', 'e'));
const result = a.validate(somevalue);
if (result.type === 'success') {
const value : 'a' | 'b' | 'c' | 'd' | 'e' = result.value;
}
If more than 8 variants are provided to Enum
, the type will now be string
(used to be unknown
).
Typesafe-schema v0.4.6
Add Dict
schema to validate input where keys are strings not statically known.