io-ts codec types for parsing data
npm i io-ts-parser-types
Note. fp-ts
and io-ts
are peer dependencies for io-ts-parser-types
returns a codec that matches string with regexp and the validates all named capture groups with codec.
const dataCodec = t.type({
prop: NumberFromString,
value: BooleanFromString,
});
const codec = typeFromRegexp(/p(?<prop>\d+)\/v(?<value>.+)/, dataCodec);
expect(decode(codec, "p123/vtrue")).toEqual({
prop: 123,
value: true,
});
Both following codecs accept the same parameters schema
and name
. The name
is just a codec name. for Schema look for example
returns a codec that extracts fields for the position in the string and returns an object. encode returns the object with encoded fields
returns a codec that extracts fields for the position in the string and returns an object. encode returns the string with field values in respective positions
const schema = {
prop1: {
position: [1, 2],
codec: NumberFromString,
},
prop2: {
position: [5, 9],
codec: BooleanFromString,
},
};
const typeC = typeFromString(schema, "string to object");
const codecTypeC = codecTypeFromString(schema, "string to object to string");
const result = {
prop1: 2,
prop2: true,
};
expect(decode(typeC, "12345true78")).toEqual(result);
expect(decode(codecTypeC, "12345true78")).toEqual(result);
expect(typeC.encode(result)).toEqual({
prop1: "2",
prop2: "true",
});
expect(codecTypeC.encode(result)).toEqual(" 2 true");
- separator: string | RegExp
returns a codec that extracts fields for the the string with values separated by separator
and returns an object. encode returns the object with encoded fields
- separator: string
returns a codec that extracts fields for the the string with values separated by separator
and returns an object. encode returns the string with field values in respective positions
const schema = {
prop1: {
position: 1,
codec: NumberFromString,
},
prop2: {
position: 0,
codec: BooleanFromString,
},
};
const typeC = typeFromSeparatedValues("|", schema, "string to object");
const codecTypeC = codecTypeFromSeparatedValues("|", schema, "string to object to string");
const result = {
prop1: 2,
prop2: true,
};
expect(decode(typeC, "true|2")).toEqual(result);
expect(decode(codecTypeC, "true|2")).toEqual(result);
expect(typeC.encode(result)).toEqual({
prop1: "2",
prop2: "true",
});
expect(codecTypeC.encode(result)).toEqual("true|2");
- integerFrom(options) - generic codec creation function for integer values
- decimalFrom(options) - generic codec creation function for decimal values. It uses decimal.js
-
floatFrom(options) - generic codec creation function for float values
- regexp - optional, RegExp, matches string and returns first group as a number, ignores commas as a thousand separator
- name - optional, name of the codec
-
ZeroFromNull - returns
0
fornull
- DecimalFromPercentString - returns a percent value from string like e.g., 58% -> 0.58
-
booleanFrom(options) - generic boolean codec creator function.
- caseSensitive - default: true,
- true - true value, false - false value
- true - true value, strict - optional, boolean, if set to true only true value matches
- false - false value, strict - optional, boolean, if set to true only false value matches
-
nullFrom(options) - generic null codec creator function
-
match - array of values to be valid as
null
- caseSensitive - optional, default true, matches values from
match
exactly. If you want to treat strings from thematch
array as non case-sensitive set them tofalse
-
match - array of values to be valid as
- TrimmedString - validates that value is a string and trims white space.
License