Skip to content

Commit 21f36c1

Browse files
committed
Release 0.13.34
1 parent cdb9b55 commit 21f36c1

3 files changed

Lines changed: 38 additions & 18 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "pave",
33
"type": "module",
4-
"version": "0.13.33",
4+
"version": "0.13.34",
55
"author": "Casey Foster <c@sey.me>",
66
"license": "MIT",
77
"repository": {

src/execute.test.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ export default async () => {
5353
things: {
5454
type: {
5555
arrayOf: {
56-
oneOf: { b: 'Bar', f: 'Foo' },
57-
resolveType: ({ id }) => (id ? 'f' : 'b')
56+
oneOf: { Bar: 'Bar', Foo: 'Foo' },
57+
resolveType: ({ id }) => (id ? 'Foo' : 'Bar')
5858
},
5959
minLength: 1,
6060
maxLength: 10
@@ -84,12 +84,12 @@ export default async () => {
8484
},
8585
type: {
8686
oneOf: {
87-
String: { type: 'String', typeInput: { maxLength: 3 } },
87+
str: { type: 'String', typeInput: { maxLength: 3 } },
8888
ThingA,
8989
ThingB
9090
},
9191
resolveType: val =>
92-
typeof val === 'string' ? 'String' : val.a ? 'ThingA' : 'ThingB'
92+
typeof val === 'string' ? 'str' : val.a ? 'ThingA' : 'ThingB'
9393
},
9494
resolve: ({ input: { thing } }) => thing
9595
},
@@ -100,8 +100,9 @@ export default async () => {
100100
nullableArrayOf: { nullable: { arrayOf: 'String' } },
101101
nullableOneOf: {
102102
nullable: {
103-
oneOf: { string: 'String', number: 'Number' },
104-
resolveType: value => typeof value
103+
oneOf: { String: 'String', Number: 'Number' },
104+
resolveType: value =>
105+
typeof value === 'number' ? 'Number' : 'String'
105106
}
106107
},
107108
arrayOfStrings: {
@@ -140,8 +141,9 @@ export default async () => {
140141
object: {
141142
id: {
142143
type: {
143-
oneOf: { number: 'Number', string: 'String' },
144-
resolveType: value => typeof value
144+
oneOf: { Number: 'Number', String: 'String' },
145+
resolveType: value =>
146+
typeof value === 'number' ? 'Number' : 'String'
145147
}
146148
},
147149
subFoo: {
@@ -227,13 +229,13 @@ export default async () => {
227229
nullableStringF: { _: 'nullableStringArgs', $: { string: ' ' } },
228230
selfLink: { selfLinkWithAddition: { addition: {} } },
229231
things: {
230-
_on_f: {
232+
_on_Foo: {
231233
_type: {},
232234
id: {},
233235
name: { $: { separator: ' ' } },
234236
sub: { _: 'subFoo', id: {}, subSub: { _: 'subFoo', id: {} } }
235237
},
236-
_on_b: { _type: {}, color: {} }
238+
_on_Bar: { _type: {}, color: {} }
237239
},
238240
oneOfArgsString: { _: 'oneOfArgs', $: { thing: 'str' } },
239241
oneOfArgsA: {

src/validate-schema.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ export const validateSchema = ({ extensions, schema }) => {
4545
oneOf: {
4646
recursive: {},
4747
string: {
48-
resolve: ({ schema, value }) => {
48+
resolve: ({ path, schema, value }) => {
4949
const keys = Object.keys(schema);
5050
if (typeof value === 'string' && keys.includes(value)) return value;
5151

5252
throw new Error(
53-
`Expected ${JSON.stringify(value)} to be one of ${keys.join(', ')}`
53+
`Expected ${JSON.stringify(value)} at ${`"${path.join('"."')}"`} to be one of ${keys.join(', ')}`
5454
);
5555
}
5656
},
@@ -77,11 +77,29 @@ export const validateSchema = ({ extensions, schema }) => {
7777
oneOf: {
7878
type: typeObject,
7979
validate: ({ path, value }) => {
80-
if (Object.keys(value).length) return value;
81-
82-
throw new Error(
83-
`Expected ${`"${path.join('"."')}"`} to define at least one type`
84-
);
80+
if (!Object.keys(value).length) {
81+
throw new Error(
82+
`Expected ${`"${path.join('"."')}"`} to define at least one type`
83+
);
84+
}
85+
86+
for (const key in value) {
87+
const type = value[key];
88+
89+
if (typeof type === 'string') {
90+
if (key !== type) {
91+
throw new Error(
92+
`Expected "${key}" at ${`"${path.join('"."')}" to equal "${type}"`}`
93+
);
94+
}
95+
} else if (key in schema) {
96+
throw new Error(
97+
`Expected "${key}" at ${`"${path.join('"."')}" to either equal "${key}" be renamed or to avoid ambiguity`}`
98+
);
99+
}
100+
}
101+
102+
return value;
85103
}
86104
},
87105
resolveType: fn

0 commit comments

Comments
 (0)