Skip to content

Commit

Permalink
undeprecate RefinementC, refinement, Integer
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Dec 3, 2022
1 parent ffc764f commit 3335112
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 65 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,7 @@
- undeprecate `FunctionType`, `FunctionC`, `Function`
- undeprecate `NeverType`, `NeverC`, `never`
- undeprecate `AnyType`, `AnyC`, `any`
- undeprecate `RefinementC`, `refinement`, `Integer`

# 2.2.19

Expand Down
86 changes: 37 additions & 49 deletions src/index.ts
Expand Up @@ -1157,7 +1157,6 @@ export function brand<C extends Any, N extends string, B extends { readonly [K i
predicate: Refinement<TypeOf<C>, Branded<TypeOf<C>, B>>,
name: N
): BrandC<C, B> {
// tslint:disable-next-line: deprecation
return refinement(codec, predicate, name)
}

Expand Down Expand Up @@ -2062,6 +2061,43 @@ export interface AnyC extends AnyType {}
*/
export const any: AnyC = new AnyType()

/**
* @since 1.5.3
*/
export interface RefinementC<C extends Any> extends RefinementType<C, TypeOf<C>, OutputOf<C>, InputOf<C>> {}

/**
* @category combinators
* @since 1.0.0
*/
export function refinement<C extends Any>(
codec: C,
predicate: Predicate<TypeOf<C>>,
name = `(${codec.name} | ${getFunctionName(predicate)})`
): RefinementC<C> {
return new RefinementType(
name,
(u): u is TypeOf<C> => 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
// -------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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<C extends Any> extends RefinementType<C, TypeOf<C>, OutputOf<C>, InputOf<C>> {}

/**
* Use `brand` instead.
*
* @category combinators
* @since 1.0.0
* @deprecated
*/
export function refinement<C extends Any>(
codec: C,
predicate: Predicate<TypeOf<C>>,
name = `(${codec.name} | ${getFunctionName(predicate)})`
): // tslint:disable-next-line: deprecation
RefinementC<C> {
return new RefinementType(
name,
(u): u is TypeOf<C> => 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.
*
Expand Down
1 change: 0 additions & 1 deletion test/2.1.x/default-types.ts
Expand Up @@ -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'])
Expand Down
3 changes: 0 additions & 3 deletions test/2.1.x/exact.ts
Expand Up @@ -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' }))
})
Expand Down Expand Up @@ -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 } | <function1>)>'])
assertFailure(T, { foo: 'a' }, ['Invalid value {"foo":"a"} supplied to : Exact<({ foo: string } | <function1>)>'])
})

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' })
})
Expand Down
2 changes: 1 addition & 1 deletion test/2.1.x/helpers.ts
Expand Up @@ -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<T>(result: t.Validation<T>, expected: any): void {
pipe(
result,
Expand Down Expand Up @@ -95,7 +96,6 @@ export const HyphenatedString = new t.Type<string, string, unknown>(
(a) => a[0] + a[2]
)

// tslint:disable-next-line: deprecation
export const IntegerFromString = t.refinement(NumberFromString, t.Integer.is, 'IntegerFromString')

export function withDefault<T extends t.Mixed>(
Expand Down
1 change: 0 additions & 1 deletion test/2.1.x/record.ts
Expand Up @@ -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
)
Expand Down
15 changes: 5 additions & 10 deletions test/2.1.x/refinement.ts
Expand Up @@ -5,29 +5,25 @@ 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 | <function1>)')
})

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')
})
})

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)
assert.strictEqual(T.is(1), true)
})

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)
Expand All @@ -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'])
Expand All @@ -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)
})
Expand Down
1 change: 1 addition & 0 deletions test/2.1.x/union.ts
Expand Up @@ -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
Expand Down

0 comments on commit 3335112

Please sign in to comment.