Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jeengbe/vality
A TypeScript schema descriptor library with zero dependencies.
https://github.com/jeengbe/vality
eslint eslint-plugin javascript schema transformer typescript validation vality
Last synced: 20 days ago
JSON representation
A TypeScript schema descriptor library with zero dependencies.
- Host: GitHub
- URL: https://github.com/jeengbe/vality
- Owner: jeengbe
- License: mit
- Created: 2022-08-07T11:34:43.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-02-13T18:50:13.000Z (11 months ago)
- Last Synced: 2024-10-16T00:33:04.392Z (3 months ago)
- Topics: eslint, eslint-plugin, javascript, schema, transformer, typescript, validation, vality
- Language: TypeScript
- Homepage: https://ts-vality.io
- Size: 1.58 MB
- Stars: 9
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
Vality
A TypeScript schema descriptor library with zero dependencies.
See https://jeengbe.github.io/vality for more information.
## [Vality](https://npmjs.com/package/vality)
[![License](https://img.shields.io/npm/l/vality)](https://github.com/jeengbe/vality/blob/master/packages/vality/LICENSE.md)
[![Version](https://img.shields.io/npm/v/vality)](https://www.npmjs.com/package/vality)
[![Coverage Status](https://img.shields.io/codecov/c/github/jeengbe/vality/master?flag=vality&token=L0QZW59UTU)](https://app.codecov.io/gh/jeengbe/vality/tree/master/packages/vality)
[![Dependencies](https://img.shields.io/badge/dependencies-0-brightgreen)](https://github.com/jeengbe/vality/network/dependencies#packages%2Fvality%2Fpackage.json)Vality is the heart of this repository. It is a declarative **schema description library** with the most **intuitive syntax** and allows for validation and transformation of data. Then extract the types from your schema for **100% type safety**. And all with **0 runtime dependencies**.
Find all of this and much more on https://jeengbe.github.io/vality/vality.
```ts
import { v, Parse } from "vality";const Person = {
name: v.string,
age: v.number({ min: 6 }),
email: v.email,
referral: ["friends", "ad", "media", null],
languages: [["de", "en", "fr", "se"]],
} as const;type Person = Parse;
/* {
name: string;
age: number;
email: Email;
referral: "friends" | "ad" | "media" | null;
languages: ("de" | "en" | "fr" | "se")[];
} */
```Now that I have your attention, head over to https://jeengbe.github.io/vality/vality to find out what's going on here. You won't regret it ;) Or check out [packages/vality](packages/vality).
Or continue scrolling down to see what else is in this repository.
## [Vality ESLint Plugin](https://npmjs.com/package/eslint-plugin-vality)
[![License](https://img.shields.io/npm/l/eslint-plugin-vality)](https://github.com/jeengbe/vality/blob/master/packages/eslint-plugin-vality/LICENSE.md)
[![Version](https://img.shields.io/npm/v/eslint-plugin-vality)](https://www.npmjs.com/package/eslint-plugin-vality)
[![Coverage Status](https://img.shields.io/codecov/c/github/jeengbe/vality/master?flag=eslint-plugin-vality&token=L0QZW59UTU)](https://app.codecov.io/gh/jeengbe/vality/tree/master/packages/eslint-plugin-vality)```ts
const Brand = {
logo: v.dict([64, 128, 256], v.url),
};type Brand = Parse;
/* {
logo: {
[x: number]: URL; // Where are the literal values???
};
} */
```Can you spot what's wrong with this model? Correct, it's missing the literal types for the keys. This is a common mistake when using Array or Enum Shorts and can be easily fixed by adding `as const`. (Find more information on this mistake [here](https://jeengbe.github.io/vality/vality/as-const)).
```ts
const Brand = {
logo: v.dict([64, 128, 256] as const, v.url),
};type Brand = Parse;
/* {
logo: {
64: URL;
128: URL;
256: URL;
};
} */
```Forgetting this sucks and can quickly become a source of frustration when suddenly types are weird. ESLint to the rescue! It will warn you when you forget to add `as const` in places where is may backfire and adds it automatically for you.
Find more information on https://jeengbe.github.io/vality/eslint-plugin-vality or check out [packages/eslint-plugin-vality](packages/eslint-plugin-vality).
## [Vality Env](https://npmjs.com/package/vality-env)
[![License](https://img.shields.io/npm/l/vality-env)](https://github.com/jeengbe/vality/blob/master/packages/vality-env/LICENSE.md)
[![Version](https://img.shields.io/npm/v/vality-env)](https://www.npmjs.com/package/vality-env)
[![Coverage Status](https://img.shields.io/codecov/c/github/jeengbe/vality/master?flag=vality-env&token=L0QZW59UTU)](https://app.codecov.io/gh/jeengbe/vality/tree/master/packages/vality-env)Use Vality to describe your configuration and load+validate it.
```ts
import { v } from "vality";
import { loadEnv } from "vality-env";const config = {
jwt: {
privateKey: v.string,
},
db: {
url: v.env("DATABASE_URL", v.string),
databaseName: v.env("DATABASE_NAME", v.string({
default: "service"
})),
},
};export function loadConfig() {
const validatedConfig = loadEnv(config);if (!validatedConfig.valid) {
console.error(validatedConfig.errors);
throw new Error('Invalid config');
}return validatedConfig.data;
}
``````env
DATABASE_URL=http://localhost:8259
# DATABASE_NAME=
JWT_PRIVATE_KEY=asdasdasdasd
```