This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b6eb4d
commit f2485c2
Showing
11 changed files
with
262 additions
and
290 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Type } from '@sinclair/typebox' | ||
import { validate } from './validate.js' | ||
import { describe, test as it } from 'node:test' | ||
import assert from 'node:assert/strict' | ||
|
||
void describe('validate', () => { | ||
void it('Should check input is valid', async () => { | ||
const maybeValid = validate(Type.Number())(42) | ||
if ('value' in maybeValid) { | ||
assert.equal(maybeValid.value, 42) | ||
} else { | ||
throw new Error(`It should be valid!`) | ||
} | ||
}) | ||
void it("Should check as 'invalid' values less than 0", () => { | ||
const maybeValid = validate(Type.Number({ minimum: 0 }))(-42) | ||
assert.equal('error' in maybeValid, true) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { Static, TSchema } from '@sinclair/typebox' | ||
import { TypeCompiler } from '@sinclair/typebox/compiler' | ||
import { ErrorInfo, ErrorType } from './ErrorInfo.js' | ||
|
||
/** | ||
* Validate the value against the given TypeBox schema | ||
*/ | ||
export const validate = <T extends TSchema>( | ||
schema: T, | ||
): ((value: unknown) => | ||
| { value: Static<typeof schema> } | ||
| { | ||
error: ErrorInfo | ||
}) => { | ||
const C = TypeCompiler.Compile(schema) | ||
return (value: unknown) => { | ||
const firstError = C.Errors(value).First() | ||
if (firstError !== undefined) { | ||
return { | ||
error: { | ||
type: ErrorType.BadRequest, | ||
message: 'Validation failed!', | ||
detail: { | ||
errors: [...C.Errors(value)], | ||
input: value, | ||
}, | ||
}, | ||
} | ||
} | ||
return { value: value as Static<typeof schema> } | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.