If you find this useful, please don't forget to star
Follow me on Twitter and GitHub to keep updated about this project and others.
schm is a library for creating immutable, composable, parseable and validatable (yeah, many *ables) schemas in JavaScript and Node.js. That's highly inspired by functional programming paradigm.
Play with schm
on RunKit (click on Clone and edit this document at the bottom and skip login if you want).
const schema = require('schm')
const userSchema = schema({
name: String,
age: {
type: Number,
min: 18,
},
})
userSchema.parse({
name: 'Haz',
age: '27',
})
await userSchema.validate({
name: 'Jeane',
age: 10,
})
Output:
// parsed
{
name: 'Haz',
age: 27,
}
// validate error
[
{
param: 'age',
value: 10,
validator: 'min',
min: 18,
message: 'age must be greater than or equal 18',
},
]
The way you declare the schema object is very similar to mongoose Schemas. So, refer to their docs to learn more.
Packages
schm
repository is a monorepo managed by lerna. Click on package name to see specific documentation.
Package | Version | Description |
---|---|---|
schm |
The main package | |
schm-computed |
Adds computed parameters to schemas | |
schm-express |
Express middlewares to handle querystring and response body | |
schm-koa |
Koa middlewares to handle querystring and response body | |
schm-methods |
Adds methods to parsed schema objects | |
schm-mongo |
Parses values to MongoDB queries | |
schm-translate |
Translates values keys to schema keys |
Contributing
When submitting an issue, put the related package between brackets in the title:
[methods] Something wrong is not right # related to schm-methods
[translate] Something right is not wrong # related to schm-translate
Something wrong is wrong # general issue
PRs are welcome. You should have a look at lerna to understand how this repository works.
After cloning the repository, run yarn
. That will install all the project dependencies, including the packages ones.
Before submitting a PR:
- Make sure to lint the code:
yarn lint
orlerna run lint
; - Make sure tests are passing:
yarn test
orlerna run test
;
License
MIT ยฉ Diego Haz