Skip to content

Commit

Permalink
fix: Remove schemas from union() call.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Aug 29, 2021
1 parent 97ec98b commit c71191e
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 160 deletions.
1 change: 1 addition & 0 deletions optimal/CHANGELOG.md
Expand Up @@ -12,6 +12,7 @@ changelog will use the new verbiage, but may affect previous APIs.
- Updated `func()` to not be nullable by default. Instead uses `undefined`.
- Updated `instance()` to no longer accept a schema as an argument, use `instance().of()` instead.
- Updated `object()` to no longer accept a schema as an argument, use `object().of()` instead.
- Updated `union()` to no longer accept a list of schemas as an argument, use `union().of()` instead.
- Renamed `Schema#nonNullable()` method to `notNullable()`.
- Removed `Schema#key()` method.
- Removed `Schema#message()` method.
Expand Down
9 changes: 5 additions & 4 deletions optimal/src/schemas/union.ts
@@ -1,21 +1,22 @@
import { createSchema } from '../createSchema';
import { commonCriteria, unionCriteria } from '../criteria';
import { AnySchema, CommonCriterias, InferNullable, Schema } from '../types';
import { AnySchema, CommonCriterias, Schema } from '../types';

export interface UnionSchema<T> extends Schema<T>, CommonCriterias<UnionSchema<T>> {
never: () => UnionSchema<never>;
notNullable: () => UnionSchema<NonNullable<T>>;
nullable: () => UnionSchema<T | null>;
of: <I = unknown>(schemas: AnySchema[]) => UnionSchema<InferNullable<T, I>>;
// Distribute these types in the future. Currently breaks on nulls...
of: (schemas: AnySchema[]) => UnionSchema<T>;
}

export function union<T = unknown>(schemas: AnySchema[], defaultValue: T): UnionSchema<T> {
export function union<T = unknown>(defaultValue: T): UnionSchema<T> {
return createSchema<UnionSchema<T>>({
criteria: { ...commonCriteria, ...unionCriteria },
defaultValue,
type: 'union',
validateType() {
// What to do here?
},
}).of(schemas) as UnionSchema<T>;
});
}
61 changes: 31 additions & 30 deletions optimal/tests/optimal.test.ts
Expand Up @@ -21,12 +21,15 @@ describe('Optimal', () => {

// This blueprint is based on Webpack's configuration: https://webpack.js.org/configuration/
// Webpack provides a pretty robust example of how to use this library.
const primitive = union<PrimitiveType>([string(), number(), bool()], false);
const primitive = union<PrimitiveType>(false).of([string(), number(), bool()]);

const condition = union<ConditionType>(
[string(), func(), regex(), array().of(regex()), object().of(regex())],
'',
);
const condition = union<ConditionType>('').of([
string(),
func(),
regex(),
array().of(regex()),
object().of(regex()),
]);

const rule = shape({
enforce: string('post').oneOf<'post' | 'pre'>(['pre', 'post']),
Expand All @@ -36,16 +39,13 @@ describe('Optimal', () => {
parser: object().of(bool()),
resource: condition,
use: array().of(
union<object | string>(
[
string(),
shape({
loader: string(),
options: object(primitive),
}),
],
[],
),
union<object | string>([]).of([
string(),
shape({
loader: string(),
options: object(primitive),
}),
]),
),
});

Expand All @@ -65,32 +65,30 @@ describe('Optimal', () => {

const blueprint = {
context: string(process.cwd()),
entry: union<EntryType>(
[
entry: union<EntryType>([])
.of([
string(),
array().of(string()),
object().of(union([string(), array().of(string())], '')),
object().of(union('').of([string(), array().of(string())])),
func(),
],
[],
).nullable(),
])
.nullable(),
output: shape({
chunkFilename: string('[id].js'),
chunkLoadTimeout: number(120_000),
crossOriginLoading: union<CrossOriginType | false>(
[
bool(false).only(),
string('anonymous').oneOf<CrossOriginType>(['anonymous', 'use-credentials']),
],
false,
),
crossOriginLoading: union<CrossOriginType | false>(false).of([
bool(false).only(),
string('anonymous').oneOf<CrossOriginType>(['anonymous', 'use-credentials']),
]),
filename: string('bundle.js'),
hashFunction: string('md5').oneOf<HashType>(['md5', 'sha256', 'sha512']),
path: string(),
publicPath: string(),
}),
module: shape({
noParse: union<NoParseType | null>([regex(), array().of(regex()), func()], null).nullable(),
noParse: union<NoParseType | null>(null)
.of([regex(), array().of(regex()), func()])
.nullable(),
rules: array().of(rule),
}),
resolve: shape({
Expand All @@ -111,7 +109,10 @@ describe('Optimal', () => {
]),
watch: bool(false),
node: object().of(
union<NodeType | boolean>([bool(), string('mock').oneOf<NodeType>(['mock', 'empty'])], false),
union<NodeType | boolean>(false).of([
bool(),
string('mock').oneOf<NodeType>(['mock', 'empty']),
]),
),
};

Expand Down

0 comments on commit c71191e

Please sign in to comment.