Skip to content

Commit

Permalink
Work on Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Apr 29, 2024
1 parent ac6a5fc commit dbac599
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
17 changes: 5 additions & 12 deletions example/index.ts
Expand Up @@ -6,21 +6,14 @@ import { Kind, TypeRegistry, FormatRegistry } from '@sinclair/typebox'
const UnsafeByte = Type.Unsafe<number>({ type: 'byte' })

const Byte = Type.Refine(UnsafeByte)
.Check((value) => typeof value === 'number')
.Check((value) => !isNaN(value), { message: 'Must not be NaN', x: 100 })
.Check((value) => typeof value === 'number', { message: 'Expected number' })
.Check((value) => !isNaN(value), { message: 'Expected non NaN number' })
.Check((value) => value >= 0, { message: 'Must be greater than 0' })
.Check((value) => value < 256, { message: 'Must be something' })
.Check((value) => value < 256, { message: 'Must be less than 256' })
.Done()

const A = Type.Object({
x: Byte,
y: Byte,
z: Byte,
})

console.dir(A, { depth: 100 })
console.log(TypeCompiler.Code(A))
console.log(Value.Errors(Byte, 'asdsa').Take(10))
const A = Type.Array(Byte)
console.log(Value.Errors(A, [0, 2, 3, 10000]).Take(10))

// Todo: Error Tests
// Todo: Investigate Error Propogation for Refinements
4 changes: 2 additions & 2 deletions src/errors/errors.ts
Expand Up @@ -76,7 +76,7 @@ import { IsArray, IsUint8Array, IsDate, IsPromise, IsFunction, IsAsyncIterator,
// ------------------------------------------------------------------
// ValueGuard
// ------------------------------------------------------------------
import { IsRefine, IsUnknown } from '../type/guard/type'
import { IsRefine } from '../type/guard/type'
// ------------------------------------------------------------------
// ValueErrorType
// ------------------------------------------------------------------
Expand Down Expand Up @@ -199,7 +199,7 @@ export class ValueErrorIterator {
// Create
// --------------------------------------------------------------------------
function CreateRefinementError(schema: TSchema, refinement: Refinement, path: string, value: unknown): ValueError {
return { type: ValueErrorType.Refinement, schema, path, value, message: refinement.message || '' }
return { type: ValueErrorType.Refinement, schema, path, value, message: refinement.message }
}
function CreateError(errorType: ValueErrorType, schema: TSchema, path: string, value: unknown): ValueError {
return { type: errorType, schema, path, value, message: GetErrorFunction()({ errorType, path, schema, value }) }
Expand Down

0 comments on commit dbac599

Please sign in to comment.