Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
* ID FHIR Primitive Runtime Type
*/
import { Type, success, failure, identity, TypeOf } from "io-ts";
// TODO: This regex seems incorrect as well
const ID_REGEX = /[A-Za-z0-9\-\.]{1,64}/;
const isId = (m: unknown): m is string =>
typeof m === "string" && ID_REGEX.test(m);
/**
* Any combination of upper- or lower-case ASCII letters ('A'..'Z', and 'a'..'z', numerals ('0'..'9'),
* '-' and '.', with a length limit of 64 characters.
*/
export const id = new Type(
"id",
isId,
(m, c) => (isId(m) ? success(m) : failure(m, c)),
identity
);
export type id = TypeOf;
import * as io from 'io-ts';
import * as t from './color.type';
export const Color = new io.Type(
"Color",
(u: any): u is t.Color => u in [t.Color.BLACK, t.Color.WHITE, t.Color.NONE],
(i: unknown, c: io.Context) => {
if(typeof i === 'string') {
switch(i.toUpperCase()){
case t.Color.BLACK: return io.success(t.Color.BLACK);
case t.Color.WHITE: return io.success(t.Color.WHITE);
case t.Color.NONE: return io.success(t.Color.NONE);
default: return io.failure(i, c, `Unknown Color ${i}`);
}
} else return io.failure(i, c, `${i} must be a string`);
},
(u: t.Color): string => u
);
) => Type = <s>() => (
carrier: Type, O, unknown>,
prism: Prism, S>,
name: string = `Refinement<${carrier.name}>`
) =>
new Type(
name,
(u): u is S => carrier.is(u) && prism.getOption(u).isSome(),
(u, c) => carrier.validate(u, c).chain(s => prism.getOption(s).foldL(() => failure(s, c), success)),
a => carrier.encode(a)
)
</s>
* @example
* import { str, Route } from 'fp-ts-routing'
* import { some, none } from 'fp-ts/lib/Option'
*
* assert.deepStrictEqual(str('id').parser.run(Route.parse('/abc')), some([{ id: 'abc' }, new Route([], {})]))
* assert.deepStrictEqual(str('id').parser.run(Route.parse('/')), none)
* @since 0.4.0
*/
export function str(k: K): Match<{ [_ in K]: string }> {
return type(k, string)
}
/**
* @internal
*/
export const IntegerFromString = new Type(
'IntegerFromString',
(u): u is number => Int.is(u),
(u, c) =>
either.chain(string.validate(u, c), s => {
const n = +s
return isNaN(n) || !Number.isInteger(n) ? failure(s, c) : success(n)
}),
String
)
/**
* `int` matches any integer path component
*
* @example
* import { int, Route } from 'fp-ts-routing'
* import { some, none } from 'fp-ts/lib/Option'
export function eitherFromJSON(
leftCodec: L,
rightCodec: R,
name: string = `Either<${leftCodec.name}, ${rightCodec.name}>`
): EitherFromJSONC {
const leftC = t.type({
value: leftCodec
})
const rightC = t.type({
value: rightCodec
})
return new t.Type(
name,
(u): u is Either, t.TypeOf> =>
(u instanceof Left && leftCodec.is(u.value)) || (u instanceof Right && rightCodec.is(u.value)),
(u, c) =>
t.UnknownRecord.validate(u, c).chain(o => {
if (o._tag === 'Left') {
return leftC.validate(o, c).map(s => left(s.value))
} else if (o._tag === 'Right') {
return rightC.validate(o, c).map(s => right(s.value))
} else {
return t.failure(u, c)
}
}),
a => a.fold(l => jleft(leftCodec.encode(l)), a => jright(rightCodec.encode(a)))
)
}
export const JSONStringType = new t.Type(
'JSONString',
t.string.is,
(input, context) =>
either.chain(t.string.validate(input, context), (stringValue) => {
try {
JSON.parse(stringValue)
return t.success(stringValue)
} catch (error) {
return t.failure(stringValue, context, 'can not be parsed as JSON')
}
}),
String
)
export const E164PhoneNumberType = new t.Type(
'E164Number',
t.string.is,
(input, context) =>
either.chain(
t.string.validate(input, context),
(stringValue) =>
isE164NumberStrict(stringValue)
? t.success(stringValue)
: t.failure(stringValue, context, 'is not a valid e164 number')
),
String
)
export const AddressType = new t.Type(
'Address',
t.string.is,
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import * as t from 'io-ts';
export const transactionSampleRateRt = new t.Type(
'TransactionSampleRate',
t.number.is,
(input, context) => {
const value = parseFloat(input as string);
return value >= 0 && value <= 1 && parseFloat(value.toFixed(3)) === value
? t.success(value)
: t.failure(input, context);
},
t.identity
);
.map(
key =>
!props.hasOwnProperty(key)
? t.getValidationError(o[key], t.appendContext(c, key, t.never))
: undefined
)
.filter((e): e is t.ValidationError => e !== undefined);
return errors.length ? t.failures(errors) : t.success(o);
}),
loose.encode
);
}
const isDate = (v: t.mixed): v is Date => v instanceof Date;
export const DateFromString = new t.Type(
"DateFromString",
isDate,
(v, c) =>
isDate(v)
? t.success(v)
: t.string.validate(v, c).chain(s => {
const d = new Date(s);
return isNaN(d.getTime()) ? t.failure(s, c) : t.success(d);
}),
a => a.toISOString()
);
export type DateFromString = t.TypeOf;
dropPropertiesByName(value, propName);
}
}
}
export const tAlphanumeric = new t.Type(
"tAlphanumeric",
(s): s is string => typeof s === "string",
(from, to) =>
either.chain(t.string.validate(from, to), s => {
return s.match(/\W/) ? t.failure(from, to, "String must be alphanumeric") : t.success(s);
}),
s => s,
);
export const tDateTime = new t.Type(
"tDateTime",
(s): s is string => typeof s === "string",
(from, to) =>
either.chain(t.string.validate(from, to), s => {
const parsed =
s.length === 10 ? moment(s, "YYYY-MM-DD") : s.length === 19 ? moment(s, "YYYY-MM-DD HH:mm:ss") : null;
return parsed && parsed.isValid() ? t.success(s) : t.failure(from, to, "Invalid datetime");
}),
s => s,
);
export const tDelayString = new t.Type(
"tDelayString",
(s): s is string => typeof s === "string",
(from, to) =>
import { Type, mixed, tuple } from 'io-ts'
import { Natural } from './Natural'
import { PositiveRational as PositiveRationalNewtype, reduce } from '../PositiveRational'
const PR = tuple([Natural, Natural])
export const PositiveRational: Type = new Type(
'PositiveRational',
PR.is,
(m, c) => PR.validate(m, c).map(([n, d]) => reduce(n, d)),
PR.encode
)