Skip to content

winexy/fuji

Repository files navigation

version types minified minified + gzip dependencies tree-shaking

🗻 fuji

🎼  Composable schema validation utility library

Example

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
}

Troubleshooting

Infer<typeof schema> infers any

Set strictFunctionTypes mode to true in your tsconfig.json compilerOptions section

{
  "compilerOptions": {
    "strictFunctionTypes": true
  } 
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published