Skip to content

Commit 3247ae1

Browse files
committed
fix: rename union and intersection, remove odds and ends
1 parent 0156877 commit 3247ae1

17 files changed

+167
-228
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ The validation errors are detailed. Adapted from the brilliant work in `flow-run
3535
- [`t.object`](#tobject)
3636
- [`t.record(t.string(), t.number())`](#trecordtstring-tnumber)
3737
- [`t.tuple(t.string(), t.number())`](#ttupletstring-tnumber)
38-
- [`t.intersection(A, B)`](#tintersectiona-b)
39-
- [`t.union(t.string(), t.number())`](#tuniontstring-tnumber)
38+
- [`t.allOf(A, B)`](#tallofa-b)
39+
- [`t.oneOf(t.string(), t.number())`](#toneoftstring-tnumber)
4040
- [`t.alias(name, type)`](#taliasname-type)
4141
- [`t.ref(() => typeAlias)`](#tref--typealias)
4242
- [`t.Type<T>`](#ttype)
@@ -264,20 +264,20 @@ A validator that requires the value to be `Record<string, number>`.
264264
A validator that requires the value to be `[string, number]`.
265265
Accepts a variable number of arguments.
266266
267-
### `t.intersection(A, B)`
267+
### `t.allOf(A, B)`
268268
269269
A validator that requires the value to be `A & B`. Accepts a variable number of arguments, though type generation is only overloaded up to 8 arguments. For example:
270270
271271
```ts
272272
const ThingType = t.simpleObject({ name: t.string() })
273273
const CommentedType = t.simpleObject({ comment: t.string() })
274274

275-
const CommentedThingType = t.intersection(ThingType, CommentedType)
275+
const CommentedThingType = t.allOf(ThingType, CommentedType)
276276

277277
CommentedThingType.assert({ name: 'foo', comment: 'sweet' })
278278
```
279279

280-
### `t.union(t.string(), t.number())`
280+
### `t.oneOf(t.string(), t.number())`
281281

282282
A validator that requires the value to be `string | number`. Accepts a variable number of arguments, though type generation is only overloaded up to 8 arguments.
283283

src/Validation.ts

Lines changed: 9 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
import Type from './types/Type'
2-
import makeJSONError from './errorReporting/makeJSONError'
32
import { weakSetAdd, weakSetDelete, weakSetHas } from './cyclic'
43

54
export type IdentifierPath = Array<string | number | symbol>
65

76
export type ErrorTuple = [IdentifierPath, string, Type<any>]
87

98
export default class Validation<T> {
10-
input: T
9+
readonly input: T
1110

12-
path: IdentifierPath = []
11+
readonly path: IdentifierPath = []
1312

14-
prefix = ''
13+
readonly prefix: string
1514

16-
errors: ErrorTuple[] = []
15+
readonly errors: ErrorTuple[] = []
1716

1817
// Tracks whether we're in validation of cyclic objects.
1918
cyclic: WeakMap<Type<any>, WeakSet<any>> = new WeakMap()
2019

21-
constructor(input: T) {
20+
constructor(input: T, prefix = '', path?: IdentifierPath) {
2221
this.input = input
22+
this.prefix = prefix
23+
if (path) this.path.push(...path)
2324
}
2425

2526
inCycle(type: Type<any>, input: any): boolean {
@@ -47,52 +48,8 @@ export default class Validation<T> {
4748
}
4849
}
4950

50-
hasErrors(path?: IdentifierPath): boolean {
51-
if (path) {
52-
for (const [candidate] of this.errors) {
53-
if (matchPath(path, candidate)) {
54-
return true
55-
}
56-
}
57-
return false
58-
} else {
59-
return this.errors.length > 0
60-
}
61-
}
62-
63-
addError(
64-
path: IdentifierPath,
65-
expectedType: Type<any>,
66-
message: string
67-
): this {
68-
this.errors.push([path, message, expectedType])
69-
return this
70-
}
71-
72-
clearError(path: IdentifierPath | null | undefined): boolean {
73-
let didClear = false
74-
if (path) {
75-
const errors = []
76-
for (const error of this.errors) {
77-
if (matchPath(path, error[0])) {
78-
didClear = true
79-
} else {
80-
errors.push(error)
81-
}
82-
}
83-
this.errors = errors
84-
} else {
85-
didClear = this.errors.length > 0
86-
this.errors = []
87-
}
88-
return didClear
89-
}
90-
91-
resolvePath(path: IdentifierPath): any {
92-
return resolvePath(this.input, path)
93-
}
94-
toJSON(): any {
95-
return makeJSONError(this)
51+
hasErrors(): boolean {
52+
return this.errors.length > 0
9653
}
9754
}
9855

@@ -141,19 +98,3 @@ export function resolvePath(input: any, path: IdentifierPath): any {
14198
}
14299
return subject
143100
}
144-
145-
export function matchPath(
146-
path: IdentifierPath,
147-
candidate: IdentifierPath
148-
): boolean {
149-
const { length } = path
150-
if (length > candidate.length) {
151-
return false
152-
}
153-
for (let i = 0; i < length; i++) {
154-
if (candidate[i] !== path[i]) {
155-
return false
156-
}
157-
}
158-
return true
159-
}

src/intersection.spec.ts renamed to src/allOf.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import * as t from './'
22
import { expect } from 'chai'
33
import dedent from 'dedent-js'
44

5-
describe(`t.intersection`, function() {
6-
const ObjectIntersection = t.intersection(
5+
describe(`t.allOf`, function() {
6+
const ObjectIntersection = t.allOf(
77
t.simpleObject({ foo: t.number() }, { exact: false }),
88
t.simpleObject({ bar: t.string() }, { exact: false })
99
)

src/errorMessages.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const errorMessages = {
2525
ERR_EXPECT_SYMBOL: 'must be a symbol',
2626
ERR_EXPECT_THIS: 'must be exactly this',
2727
ERR_EXPECT_VOID: 'must be undefined',
28+
ERR_EXPECT_LENGTH: 'must have length of $0',
2829
ERR_INVALID_DATE: 'must be a valid date',
2930
ERR_MISSING_PROPERTY: 'must have property: $0',
3031
ERR_NO_INDEXER: 'is not one of the permitted indexer types',
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { keyToString } from './keyToString'
2+
import { expect } from 'chai'
3+
4+
describe(`keyToString`, function() {
5+
it(`formats symbol correctly`, function() {
6+
expect(keyToString(Symbol('foo'))).to.equal('[Symbol(foo)]')
7+
})
8+
it(`formats number correctly`, function() {
9+
expect(keyToString(2)).to.equal('2')
10+
})
11+
it(`formats non-identifiers correctly`, function() {
12+
expect(keyToString('2pac')).to.equal('"2pac"')
13+
expect(keyToString('-foobus-')).to.equal('"-foobus-"')
14+
})
15+
})

src/errorReporting/keyToString.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function keyToString(key: string | number | symbol): string {
2+
switch (typeof key) {
3+
case 'symbol':
4+
return `[${String(key)}]`
5+
case 'number':
6+
return String(key)
7+
case 'string':
8+
if (/^[_a-z][_a-z0-9]*$/i.test(key)) return key
9+
}
10+
return JSON.stringify(key)
11+
}

src/errorReporting/makeJSONError.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/errorReporting/makeWarningMessage.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/errorReporting/typeOf.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
export function keyToString(key: string | number | symbol): string {
2-
switch (typeof key) {
3-
case 'symbol':
4-
return `[${String(key)}]`
5-
case 'number':
6-
return String(key)
7-
case 'string':
8-
if (/^[_a-z][_a-z0-9]*$/i.test(key)) return key
9-
}
10-
return JSON.stringify(key)
11-
}
1+
import { keyToString } from './keyToString'
122

133
export default function typeOf(value: any): string {
144
if (value == null) return String(value)

src/index.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const DeployConfigType = t.object<{
5454
* The subnet ids for the Vpc. Defaults to all available for VpcId
5555
*/
5656

57-
Redis: t.union(
57+
Redis: t.oneOf(
5858
t.object<{
5959
Type: any
6060
Host: any
@@ -73,7 +73,7 @@ const DeployConfigType = t.object<{
7373
AvailabilityZone: t.string(),
7474
})
7575
),
76-
DB: t.union(
76+
DB: t.oneOf(
7777
t.object<{
7878
Type: any
7979
Host: any
@@ -235,7 +235,7 @@ const DeployConfigType = t.object<{
235235
LambdaFunctionName: t.optionalNullOr(t.string()),
236236
})
237237
),
238-
CloudFormationTemplateBucket: t.optionalNullOr(t.union(t.string(), t.null())),
238+
CloudFormationTemplateBucket: t.optionalNullOr(t.oneOf(t.string(), t.null())),
239239
JCoreIOLink: t.optionalNullOr(t.string()),
240240
Superadmin: t.optionalNullOr(
241241
t.object<{

0 commit comments

Comments
 (0)