https://github.com/uppercod/schema
https://github.com/uppercod/schema
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/uppercod/schema
- Owner: UpperCod
- Created: 2020-11-20T14:28:56.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-12-10T00:31:19.000Z (over 5 years ago)
- Last Synced: 2025-03-16T19:51:42.990Z (over 1 year ago)
- Language: JavaScript
- Size: 35.2 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @uppercod/schema
declare validations, filters or transformers in a simple and functional.
## install
```
npm install @uppercod/schema
```
## Usage
### Simple
```js
import { schema, error } from "@uppercod/schema";
const transform = schema({
age: Number,
email: (value) => {
if (/\w+@\w+.\w+$/.test(value)) {
return value;
}
throw "invalid email";
},
});
transform({ age: "20" }).then(
(valid) => console.log("valid: ", valid),
(invalid) => console.log("invalid: ", invalid)
);
```
### multiple validations in per field
```js
const min = (minValue) => (value) =>
minValue <= value ? error`The ${value} is less than ${minValue}` : value;
const transform = schema({
age: [Number, min(3)],
});
```
### nested
```js
const trim = (value) => value.trim();
const user = schema({
id: Number,
userName: trim,
});
const data = schema({
user: user,
});
```
### context
```js
const user = schema({
firstName: String,
lastName: String,
fullName: (value, prop, { fistName, lastName }) =>
fistName + " " + lastName,
});
```
## filters
```js
import is from "is_js";
import { schema } from "@uppercod/schema";
import { filter, date, options } from "@uppercod/schema/filter";
schema({
email: filter(is.email),
date: date({ min: new Date("01-02-2020") }),
status: options("active", "inactive"),
});
```
## Api 2
```js
import { schema, optional, use } from "@uppercod/schema";
schema({
id: (value, field, context) => {},
firstName : minLength(3)
fullName: () => {
const {firstName,lastName} = use("valid");
return firstName + " "+ lastName;
},
});
```