Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/apendua/very-simple-schema
A minimalistic implementation of SimpleSchma most useful features
https://github.com/apendua/very-simple-schema
form-validation form-validation-constructor json-validation meteor schema
Last synced: 2 months ago
JSON representation
A minimalistic implementation of SimpleSchma most useful features
- Host: GitHub
- URL: https://github.com/apendua/very-simple-schema
- Owner: apendua
- License: mit
- Created: 2017-01-16T12:04:50.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-08T16:21:12.000Z (about 6 years ago)
- Last Synced: 2024-09-30T03:05:48.585Z (3 months ago)
- Topics: form-validation, form-validation-constructor, json-validation, meteor, schema
- Language: JavaScript
- Homepage:
- Size: 371 KB
- Stars: 5
- Watchers: 5
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# very-simple-schema
Bundle size: ~22.5 kB, ~5.5 kB gzipped
[![Build Status](https://travis-ci.org/apendua/very-simple-schema.svg?branch=master)](https://travis-ci.org/apendua/very-simple-schema)
## Like SimpleSchema but even simpler
This package implements an essential subset of [SimpleSchema](https://github.com/aldeed/node-simple-schema).
Unlike the original version it does not provide validation of Mongo selectors, nor does it allow you to define default and auto values.
Despite the limited functionality it may still be an interesting alternative for solving problems like:- form validation - it integrates nicely with [redux-form](https://github.com/erikras/redux-form)
- function parameters validation, e.g. in remote api calls## Instllation
```
npm install --save very-simple-schema
```## Basic usage
```javascript
import { Schema } from 'very-simple-schema';const Book = new Schema({
author: { type: String, nonEmpty: true }, // must be present and non-empty
title: { type: String, optional: true, nonEmpty: true }, // can be missing, but if present then must be non-empty
abstract: { type: String, optional: true }, // can be missing
signature: { type: String }, // must be present, if present can be empty
});
```## Gotchas
Unlkie in `SimpleSchema`, empty strings are accepted even if a property is required. The reason `SimpleSchema` treats them
differently is that `clean` method is being called on an object before it's validated. To emulate the original behavior
you can create a custom `Schema` class with different default behavior:
```javascript
import { createSchema, presetDefault } from 'very-simple-schema';const Schema = createSchema({
plugins: [
...presetDefault,
],
emptyStringsAreMissingValues: true,
});
```## Examples
```javascript
import { Schema } from 'very-simple-schema';// a number between 0 and 10
new Schema(Number, { min: 0, max: 10 });// array with at least element
Schema.arrayOf(Schema.any(), { minCount: 1 });// object that contains anything
Schema.blackbox();
Schema.objectOf(Schema.any());// either Yes or No
Schema.enum(['Yes', 'No']);// can be one of the specified types
const Id = Schema.oneOf([String, Number]);const User = new Schema({
name: { type: String },
email: { type: String, regEx: Schema.RegEx.Email, optional: true },
}, {
// custom typeName can improve some error messages
typeName: 'User',
});const Book = new Schema({
id: { type: Id },
// type can reference another schema
author: { type: User },
// strings can be requested to be non empty
title: { type: String, nonEmpty: true },
abstract: { type: String },
// array can be constructed with [] shortcut, maxCount refers to array lenght, max refers to string length
chapters: { type: [String], '$.max': 128, maxCount: 10 },
}, { typeName: 'Book' });const Library = new Schema({
books: { type: [Book], maxCount: 1000 },
}, {
typeName: 'Library',
});
```