@hello.nrfcloud.com/proto
Version:
Documents the communication protocol between the hello.nrfcloud.com backend and the web application
25 lines (23 loc) • 624 B
text/typescript
import type { Static, TSchema } from '@sinclair/typebox'
import { TypeCompiler, type ValueError } from '@sinclair/typebox/compiler'
/**
* Validate the value against the given TypeBox schema
*/
export const validateWithTypeBox = <T extends TSchema>(
schema: T,
): ((value: unknown) =>
| { value: Static<typeof schema> }
| {
errors: ValueError[]
}) => {
const C = TypeCompiler.Compile(schema)
return (value: unknown) => {
const firstError = C.Errors(value).First()
if (firstError !== undefined) {
return {
errors: [...C.Errors(value)],
}
}
return { value: value as Static<typeof schema> }
}
}