jet-schema
Advanced tools
Comparing version 1.2.1 to 1.2.2
{ | ||
"name": "jet-schema", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Simple, typescript-first schema validation tool", | ||
@@ -8,5 +8,9 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"clean-install": "rm -rf ./node_modules && rm -r package-lock.json && npm i", | ||
"test": "NODE_ENV=test vitest", | ||
"test:pg": "ts-node ./test/playground.ts", | ||
"build": "rm -rf ./dist && tsc -p tsconfig.build.json", | ||
"test:min:mjs": "node ./test/test-es6-min.mjs", | ||
"test:min:cjs": "node ./test/test-cjs-min.js", | ||
"build": "rm -rf ./dist && tsc -p tsconfig.build.json && npm run bundle", | ||
"bundle": "rollup --bundleConfigAsCjs --config rollup.config.ts --configPlugin typescript", | ||
"lint": "eslint --flag unstable_ts_config" | ||
@@ -39,2 +43,4 @@ }, | ||
"@eslint/js": "^9.11.1", | ||
"@rollup/plugin-terser": "^0.4.4", | ||
"@rollup/plugin-typescript": "^12.1.1", | ||
"@stylistic/eslint-plugin-ts": "^2.8.0", | ||
@@ -45,3 +51,6 @@ "@types/eslint__js": "^8.42.3", | ||
"eslint-plugin-n": "^17.10.3", | ||
"i": "^0.3.7", | ||
"jiti": "^2.3.3", | ||
"npm": "^10.9.0", | ||
"rollup": "^4.24.3", | ||
"ts-node": "^10.9.2", | ||
@@ -52,6 +61,3 @@ "tslib": "^2.8.0", | ||
"vitest": "^2.1.4" | ||
}, | ||
"dependencies": { | ||
"zod": "^3.23.8" | ||
} | ||
} |
# Jet-Schema ✈️ | ||
> Simple, zero-dependency, typescript-first schema validation tool, that lets you integrate your own validator-functions. | ||
> Simple, zero-dependency, typescript-first schema validation tool, with a focus on using your own custom validator functions. | ||
@@ -36,4 +36,4 @@ | ||
- TypeScript first! | ||
- Quick, terse, simple, easy-to-use (this library only exports 2 functions and 2 types). | ||
- Much smaller and less complex than most schema-validation libraries. | ||
- Quick, terse, simple, small (this library only exports 2 functions and 2 types, minified size is **4.56 KB**). | ||
- Less complex than nearly every other schema-validation library. | ||
- Typesafety works both ways, you can either force a schema structure using a pre-defined type OR you can infer a type from a schema. | ||
@@ -67,2 +67,3 @@ - `new`, `test`, `parse` functions provided automatically on every new schema. | ||
// NOTE: we can also infer the type from the schema | ||
const User = schema<IUser>({ | ||
@@ -88,11 +89,9 @@ id: isNumber, | ||
Note that at the heart of `jet-schema` are **validator-functions**. Since functions are objects in javascript, and objects are pass by reference, `jet-schema` will map certain settings to validator-functions by using the functions themselves as a reference. This is what enables you to pass your existing validator-functions to your schema without have to deal with the library's features for every object-property. | ||
> npm install -s jet-schema | ||
After installation, you need to configure the `schema` function by importing and calling `jetSchema()`. | ||
After installation, you need to configure the `schema` function by importing and calling `jetSchema()`. When setting up **jet-schema** for the first time, usually what I do is create two files in my `util/` folder: `schema.ts` and `validators.ts`. In `schema.ts` I'll import and call the `jet-schema` function then apply any globals and a custom clone-function. If you don't want to go through this step, you can import the `schema` function directly from `jet-schema`. | ||
<br/> | ||
`jetSchema()` accepts an optional settings object with 3 three optional properties: | ||
- `globals`: An array of **validator-objects**, which set certain default settings for specific validator-functions. You should use this option for frequently used validator-function/default/transform combinations where you don't want to set a default value or transform-function every time. Upon initialization, the validator-functions will check their defaults. If a value is not optional and you do not supply a default value, then an error will occur when the schema is initialized. If you don't set a default value for a validator-function in `jetSchema()`, you can also do so when setting up an individual schema (the next 3 snippets below contain examples).<br/> | ||
- `globals`: An array of **validator-objects**, which map certain global settings for specific validator-functions. These functions are passed by reference so you need to make sure you reuse the same functions (not a copy of those functions) when setting up a particular schema. You should use this option for frequently used validator-function/default/transform combinations where you don't want to set a default value or transform-function every time. Upon initialization, the validator-functions will check their defaults. If a value is not optional and you do not supply a default value, then an error will occur when the schema is initialized. If you don't set a default value for a validator-function in `jetSchema()`, you can also do so when setting up an individual schema (the next 3 snippets below contain examples).<br/> | ||
@@ -109,5 +108,6 @@ The format for a **validator-object** is: | ||
- `cloneFn`: A custom clone-function if you don't want to use the built-in function which uses `structuredClone` (I like to use `lodash.cloneDeep`). | ||
- `onError`: A global error handler. By default, if a validator-function fails then an error is thrown. You can override this behavior by passing a custom error handling function as the third argument. This feature is really useful for testing when you may want to return an error string instead of throw an error. Format, `(property: string, value?: unknown, origMessage?: string, schemaId?: string) => void;`. | ||
- `onError`: A global error handler. By default, if a validator-function fails then an error is thrown. You can override this behavior by passing a custom error handling function. This feature is really useful for testing when you may want to print an error string instead of throw an error. Format: `(property: string, value?: unknown, origMessage?: string, schemaId?: string) => void;`. | ||
<br/> | ||
When setting up **jet-schema** for the first time, usually what I do is create two files under my `util/` folder: `schema.ts` and `validators.ts`. In `schema.ts` I'll import and call the `jet-schema` function then apply any globals and a custom clone-function. If you don't want to go through this step, you can import the `schema` function directly from `jet-schema`. | ||
`util/validators.ts` and `util/schema.ts` overview: | ||
@@ -141,3 +141,3 @@ ```typescript | ||
Now that we have our `schema` function setup, let's make a schema. Simply import the `schema` function from `util/schema.ts` and your existing validator-functions, then pass them as the value to each property in the `schema` function or use a validator-object. The format for a validator-object is the same both locally and globally (see above). All local-settings will for a validator-function will overwrite the global ones. Remember that if a property is required then a default must be set for its validator-function (locally or globally) or else `new` won't know what to use as a value when passing a partial.<br/> | ||
Now that we have our `schema` function setup, let's make a schema. Simply import the `schema` function from `util/schema.ts` and your existing validator-functions, then pass them as the value to each property in the `schema` function or use a validator-object. The format for a validator-object is the same both locally and globally (see above). All local settings for a validator-function will overwrite the global ones. Remember that if a property is required then a default must be set for its validator-function (locally or globally) or else `new` won't know what to use as a value when passing a partial.<br/> | ||
@@ -182,7 +182,7 @@ For handling an individual schema's type, there are two ways to go about this, enforcing a schema from a type or infering a type from a schema. I'll show you an example of doing it both ways. | ||
- `pick` allows you to select any property and returns an object with the `test` and `default` functions. | ||
- `parse` is like a combination of `new` and `test`. It accepts an `unknown` value which is not optional, validates the properties but returns a new instance (while removing an extra ones) instead of a type-predicate. If you have an incoming unknown value (i.e. an api call) and you want to validate the properties and return a new cleaned instance, use `parse`. Note: only objects will pass the `parse` function, even if a schema is nullish, `null/undefined` values will not pass. | ||
- `parse` is like a combination of `new` and `test`. It accepts an `unknown` value which is not optional, validates the properties but returns a new instance (while removing an extra ones) instead of a type-predicate. Note: only objects will pass the `parse` function, even if a schema is nullish, `null/undefined` values will not pass. | ||
### Making schemas optional/nullable <a name="making-schemas-opt-null"></a> | ||
In addition to a schema-object, the `schema` function accepts an additional **options** object parameter. The values here are type-checked against the generic (`schema<"The Generic">(...)`) that was passed so you must use the correct values. If your generic is optional/nullable then your are required to pass the object so at runtime the correct values are parsed. | ||
In addition to a schema-object, the `schema` function accepts an additional **options** object parameter. The values here are type-checked against the generic (`schema<"The Generic">(...)`) that was passed so you must use the correct values. If your generic is optional/nullable then your are required to pass the object so at runtime the correct values are used. | ||
```typescript | ||
@@ -262,3 +262,3 @@ { | ||
### Combining Schemas <a name="combining-schemas"></a> | ||
For whatever reason, your schema may end up existing in multiple places. If you want to declare part of a schema that will be used elsewhere, you can import the `TJetSchema` type and use it to setup one, then merge it with your full schema later. | ||
If you want to declare part of a schema that will be used elsewhere, you can import the `TJetSchema` type and use it to setup a partial schema, then merge it with your full schema later. | ||
@@ -265,0 +265,0 @@ ```typescript |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
80437
0
15
683
17
- Removedzod@^3.23.8
- Removedzod@3.23.8(transitive)