Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show error value type when TypeError #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ Schema.extend('any', (data) => {
})

Schema.extend('never', (data) => {
throw new TypeError(`expected nullable but got ${data}`)
throw new TypeError(`expected nullable but got ${typeof data} ${data}`)
})

Schema.extend('const', (data, { value }) => {
Expand All @@ -256,13 +256,13 @@ function checkWithinRange(data: number, meta: Schema.Meta<any>, description: str
}

Schema.extend('string', (data, { meta }) => {
if (typeof data !== 'string') throw new TypeError(`expected string but got ${data}`)
if (typeof data !== 'string') throw new TypeError(`expected string but got ${typeof data} ${data}`)
checkWithinRange(data.length, meta, 'string length')
return [data]
})

Schema.extend('number', (data, { meta }) => {
if (typeof data !== 'number') throw new TypeError(`expected number but got ${data}`)
if (typeof data !== 'number') throw new TypeError(`expected number but got ${typeof data} ${data}`)
checkWithinRange(data, meta, 'number')
const { step } = meta
if (step) {
Expand All @@ -276,15 +276,15 @@ Schema.extend('number', (data, { meta }) => {

Schema.extend('boolean', (data) => {
if (typeof data === 'boolean') return [data]
throw new TypeError(`expected boolean but got ${data}`)
throw new TypeError(`expected boolean but got ${typeof data} ${data}`)
})

Schema.extend('bitset', (data, { bits }) => {
if (typeof data === 'number') return [data]
if (!Array.isArray(data)) throw new TypeError(`expected array but got ${data}`)
if (!Array.isArray(data)) throw new TypeError(`expected array but got ${typeof data} ${data}`)
let result = 0
for (const value of data) {
if (typeof value !== 'string') throw new TypeError(`expected string but got ${value}`)
if (typeof value !== 'string') throw new TypeError(`expected string but got ${typeof value} ${value}`)
if (!(value in bits)) throw new TypeError(`unknown value ${value}`)
result |= bits[value]
}
Expand All @@ -293,12 +293,12 @@ Schema.extend('bitset', (data, { bits }) => {

Schema.extend('function', (data) => {
if (typeof data === 'function') return [data]
throw new TypeError(`expected function but got ${data}`)
throw new TypeError(`expected function but got ${typeof data} ${data}`)
})

Schema.extend('is', (data, { callback }) => {
if (data instanceof callback) return [data]
throw new TypeError(`expected ${callback.name} but got ${data}`)
throw new TypeError(`expected ${callback.name} but got ${typeof data} ${data}`)
})

function property(data: any, key: keyof any, schema?: Schema) {
Expand All @@ -308,13 +308,13 @@ function property(data: any, key: keyof any, schema?: Schema) {
}

Schema.extend('array', (data, { inner, meta }) => {
if (!Array.isArray(data)) throw new TypeError(`expected array but got ${data}`)
if (!Array.isArray(data)) throw new TypeError(`expected array but got ${typeof data} ${data}`)
checkWithinRange(data.length, meta, 'array length')
return [data.map((_, index) => property(data, index, inner))]
})

Schema.extend('dict', (data, { inner, sKey }, strict) => {
if (!isPlainObject(data)) throw new TypeError(`expected object but got ${data}`)
if (!isPlainObject(data)) throw new TypeError(`expected object but got ${typeof data} ${data}`)
const result = {}
for (const key in data) {
let rKey: string
Expand All @@ -332,7 +332,7 @@ Schema.extend('dict', (data, { inner, sKey }, strict) => {
})

Schema.extend('tuple', (data, { list }, strict) => {
if (!Array.isArray(data)) throw new TypeError(`expected array but got ${data}`)
if (!Array.isArray(data)) throw new TypeError(`expected array but got ${typeof data} ${data}`)
const result = list.map((inner, index) => property(data, index, inner))
if (strict) return [result]
result.push(...data.slice(list.length))
Expand All @@ -347,7 +347,7 @@ function merge(result: any, data: any) {
}

Schema.extend('object', (data, { dict }, strict) => {
if (!isPlainObject(data)) throw new TypeError(`expected object but got ${data}`)
if (!isPlainObject(data)) throw new TypeError(`expected object but got ${typeof data} ${data}`)
const result = {}
for (const key in dict) {
const value = property(data, key, dict[key])
Expand Down