diff --git a/CHANGELOG.md b/CHANGELOG.md index 77e02838..4a4c19b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - undeprecate `FunctionType`, `FunctionC`, `Function` - undeprecate `NeverType`, `NeverC`, `never` - undeprecate `AnyType`, `AnyC`, `any` +- undeprecate `RefinementC`, `refinement`, `Integer` # 2.2.19 diff --git a/src/index.ts b/src/index.ts index eec41d87..364b7724 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1157,7 +1157,6 @@ export function brand, Branded, B>>, name: N ): BrandC { - // tslint:disable-next-line: deprecation return refinement(codec, predicate, name) } @@ -2062,6 +2061,43 @@ export interface AnyC extends AnyType {} */ export const any: AnyC = new AnyType() +/** + * @since 1.5.3 + */ +export interface RefinementC extends RefinementType, OutputOf, InputOf> {} + +/** + * @category combinators + * @since 1.0.0 + */ +export function refinement( + codec: C, + predicate: Predicate>, + name = `(${codec.name} | ${getFunctionName(predicate)})` +): RefinementC { + return new RefinementType( + name, + (u): u is TypeOf => codec.is(u) && predicate(u), + (i, c) => { + const e = codec.validate(i, c) + if (isLeft(e)) { + return e + } + const a = e.right + return predicate(a) ? success(a) : failure(a, c) + }, + codec.encode, + codec, + predicate + ) +} + +/** + * @category primitives + * @since 1.0.0 + */ +export const Integer = refinement(number, Number.isInteger, 'Integer') + // ------------------------------------------------------------------------------------- // deprecated // ------------------------------------------------------------------------------------- @@ -2215,54 +2251,6 @@ export interface ObjectC extends ObjectType {} // tslint:disable-next-line: deprecation export const object: ObjectC = new ObjectType() -/** - * Use `BrandC` instead. - * - * @since 1.5.3 - * @deprecated - */ -export interface RefinementC extends RefinementType, OutputOf, InputOf> {} - -/** - * Use `brand` instead. - * - * @category combinators - * @since 1.0.0 - * @deprecated - */ -export function refinement( - codec: C, - predicate: Predicate>, - name = `(${codec.name} | ${getFunctionName(predicate)})` -): // tslint:disable-next-line: deprecation -RefinementC { - return new RefinementType( - name, - (u): u is TypeOf => codec.is(u) && predicate(u), - (i, c) => { - const e = codec.validate(i, c) - if (isLeft(e)) { - return e - } - const a = e.right - return predicate(a) ? success(a) : failure(a, c) - }, - codec.encode, - codec, - predicate - ) -} - -/** - * Use `Int` instead. - * - * @category primitives - * @since 1.0.0 - * @deprecated - */ -// tslint:disable-next-line: deprecation -export const Integer = refinement(number, Number.isInteger, 'Integer') - /** * Use `record` instead. * diff --git a/test/2.1.x/default-types.ts b/test/2.1.x/default-types.ts index 49bf9982..87597441 100644 --- a/test/2.1.x/default-types.ts +++ b/test/2.1.x/default-types.ts @@ -139,7 +139,6 @@ describe('bigint', () => { describe('Integer', () => { it('should validate integers', () => { - // tslint:disable-next-line: deprecation const T = t.Integer assertSuccess(T.decode(1)) assertFailure(T, 0.5, ['Invalid value 0.5 supplied to : Integer']) diff --git a/test/2.1.x/exact.ts b/test/2.1.x/exact.ts index b4694e2d..a44ebdd0 100644 --- a/test/2.1.x/exact.ts +++ b/test/2.1.x/exact.ts @@ -52,7 +52,6 @@ describe('exact', () => { }) it('should succeed validating a valid value (refinement)', () => { - // tslint:disable-next-line: deprecation const T = t.exact(t.refinement(t.type({ foo: t.string }), (p) => p.foo.length > 2)) assertSuccess(T.decode({ foo: 'foo' })) }) @@ -107,14 +106,12 @@ describe('exact', () => { }) it('should fail validating an invalid value (refinement)', () => { - // tslint:disable-next-line: deprecation const T = t.exact(t.refinement(t.type({ foo: t.string }), (p) => p.foo.length > 2)) assertFailure(T, null, ['Invalid value null supplied to : Exact<({ foo: string } | )>']) assertFailure(T, { foo: 'a' }, ['Invalid value {"foo":"a"} supplied to : Exact<({ foo: string } | )>']) }) it('should strip additional properties (refinement)', () => { - // tslint:disable-next-line: deprecation const T = t.exact(t.refinement(t.type({ foo: t.string }), (p) => p.foo.length > 2)) assertSuccess(T.decode({ foo: 'foo', bar: 1 }), { foo: 'foo' }) }) diff --git a/test/2.1.x/helpers.ts b/test/2.1.x/helpers.ts index de773352..df441ec7 100644 --- a/test/2.1.x/helpers.ts +++ b/test/2.1.x/helpers.ts @@ -4,6 +4,7 @@ import * as t from '../../src/index' import { PathReporter } from '../../src/PathReporter' import { pipe } from 'fp-ts/lib/pipeable' +// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types export function assertStrictEqual(result: t.Validation, expected: any): void { pipe( result, @@ -95,7 +96,6 @@ export const HyphenatedString = new t.Type( (a) => a[0] + a[2] ) -// tslint:disable-next-line: deprecation export const IntegerFromString = t.refinement(NumberFromString, t.Integer.is, 'IntegerFromString') export function withDefault( diff --git a/test/2.1.x/record.ts b/test/2.1.x/record.ts index 1ad02f32..736e5583 100644 --- a/test/2.1.x/record.ts +++ b/test/2.1.x/record.ts @@ -150,7 +150,6 @@ describe('record', () => { const value1 = { aa: 1 } assertStrictEqual(T1.decode(value1), value1) const T2 = t.record( - // tslint:disable-next-line: deprecation t.refinement(t.string, (s) => s.length >= 2), t.number ) diff --git a/test/2.1.x/refinement.ts b/test/2.1.x/refinement.ts index 8e303f9a..5d030b9c 100644 --- a/test/2.1.x/refinement.ts +++ b/test/2.1.x/refinement.ts @@ -5,13 +5,11 @@ import { assertSuccess, assertFailure, assertStrictEqual, IntegerFromString, Num describe('refinement', () => { describe('name', () => { it('should assign a default name', () => { - // tslint:disable-next-line: deprecation const T = t.refinement(t.number, (n) => n >= 0) assert.strictEqual(T.name, '(number | )') }) it('should accept a name', () => { - // tslint:disable-next-line: deprecation const T = t.refinement(t.number, (n) => n >= 0, 'T') assert.strictEqual(T.name, 'T') }) @@ -19,7 +17,6 @@ describe('refinement', () => { describe('is', () => { it('should check a isomorphic value', () => { - // tslint:disable-next-line: deprecation const T = t.Integer assert.strictEqual(T.is(1.2), false) assert.strictEqual(T.is('a'), false) @@ -27,7 +24,6 @@ describe('refinement', () => { }) it('should check a prismatic value', () => { - // tslint:disable-next-line: deprecation const T = t.refinement(NumberFromString, (n) => n % 1 === 0) assert.strictEqual(T.is(1.2), false) assert.strictEqual(T.is('a'), false) @@ -37,21 +33,22 @@ describe('refinement', () => { describe('decode', () => { it('should succeed validating a valid value', () => { - // tslint:disable-next-line: deprecation const T = t.refinement(t.number, (n) => n >= 0) assertSuccess(T.decode(0)) assertSuccess(T.decode(1)) }) it('should return the same reference if validation succeeded', () => { - // tslint:disable-next-line: deprecation - const T = t.refinement(t.Dictionary, () => true) + const T = t.refinement( + // tslint:disable-next-line: deprecation + t.Dictionary, + () => true + ) const value = {} assertStrictEqual(T.decode(value), value) }) it('should fail validating an invalid value', () => { - // tslint:disable-next-line: deprecation const T = t.Integer assertFailure(T, 'a', ['Invalid value "a" supplied to : Integer']) assertFailure(T, 1.2, ['Invalid value 1.2 supplied to : Integer']) @@ -66,13 +63,11 @@ describe('refinement', () => { describe('encode', () => { it('should encode a prismatic value', () => { - // tslint:disable-next-line: deprecation const T = t.refinement(t.array(NumberFromString), () => true) assert.deepStrictEqual(T.encode([1]), ['1']) }) it('should return the same reference while encoding', () => { - // tslint:disable-next-line: deprecation const T = t.refinement(t.array(t.number), () => true) assert.strictEqual(T.encode, t.identity) }) diff --git a/test/2.1.x/union.ts b/test/2.1.x/union.ts index d340cba0..c7a36402 100644 --- a/test/2.1.x/union.ts +++ b/test/2.1.x/union.ts @@ -199,6 +199,7 @@ describe('union', () => { } ) assert.strictEqual( + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error t.getTags(t.intersection([t.type({ a: t.literal('a') }), t.type({ a: t.literal('b') })])), t.emptyTags