schemy
v3.3.1
Published
A (very) simple, lightweight schema validation library
Downloads
35
Maintainers
Readme
Docs 📖 · Plugins 🧩 · Changelog 📝 · Donate 💰
Schemy is an extremely simple, lightweight yet powerful schema validation library. Perfect for lightweight-oriented projects like cloud functions where size and speed are key features. It weights less than 18 KB!
Usage
Install using npm: npm install --save schemy
.
Then, create a schema with the desired properties and their types:
const Schemy = require('schemy');
const characterSchema = new Schemy({
'name': {
type: String,
required: true
}
});
// Validate against input data
if (!characterSchema.validate(someData)) {
characterSchema.getValidationErrors(); // => [ 'Missing required property name' ]
}
// You can also validate asynchronously
await Schemy.validate(someData, characterSchema);
Plugins
Schemy can be easily extended with new functionality. For example, this adds a feature to reference properties within the schema.
// Require the plugin
const ReferenceSupport = require('schemy-reference-support');
// Call Schemy.extend() with the plugin or with an array of plugins
Schemy.extend(ReferenceSupport);
new Schemy({
password: String,
confirm: Schemy.$ref('password')
});
You can check the whole list of available plugins in the wiki ↗
API
Static methods
- Schemy(object) - Takes an object with the desired structure to validate later
- Schemy.validate(data, schema) - Asynchronously validates some data against the passed schema
- Schemy.extend(Plugin) - Load one or multiple plugins into Schemy
Instance methods
- validate(data) - Validates the schema
- getValidationErrors() - Returns generated errors from the last validation
- getBody(includeAll = false) - Returns the last validated input