🎼 Composable schema validation utility library
import { f, run, string, int, required, oneOf, pattern, nullable } from '@winexy/fuji'
import type { Infer } from '@winexy/fuji'
const urlRegex = /.../
const schema = f.shape({
name: f(string(), required()),
version: f(pattern(/\d+\.\d+.\d+/)),
workspaces: f.array(f(string())),
repository: f.shape({
type: f(string(), required(), oneOf(['git', 'vcs']), nullable()),
url: f(string(), pattern(urlRegex))
})
})
type PackageType = Infer<typeof schema>
/*
{
name: string,
version?: string,
workspaces?: string[],
repository?: {
type: string | null,
url?: string
}
}
*/
const result = run(schema, {
name: '@winexy/fuji',
version: '0.0.0',
repository: {
type: 'git',
url: 'https://github.com/winexy/fuji'
}
})
if (result.invalid) {
result.errors // Array<VError>
result.value // null
} else {
result.errors // null
result.value // same as PackageType
}
Set strictFunctionTypes
mode to true
in your tsconfig.json
compilerOptions
section
{
"compilerOptions": {
"strictFunctionTypes": true
}
}