From 01373429f2f6d37e2cba143878da38117133bb88 Mon Sep 17 00:00:00 2001 From: Maurits Rijk Date: Tue, 9 May 2017 13:29:01 +0200 Subject: [PATCH] Add example to README --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index b05186b..127c06f 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,35 @@ NodeJS version of [clojure.spec](http://clojure.org/about/spec) `npm install speculaas` +## Example + +Example: + +```js +const s = require('speculaas'); +const {isString} = s.utils; + +const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$/ + +s.def('emailType', s.and(isString, s => emailRegex.test(s))); + +s.def('first-name', isString); +s.def('last-name', isString); +s.def('email', 'emailType'); +s.def('phone', isString); + +// First name, last name and email are required. Phone number is optional +s.def('person', s.keys({req: ['first-name', 'last-name', 'email'], opt: ['phone']})); + +s.isValid('::person', +{ + 'first-name': 'Elon', + 'last-name': 'Musk', + 'email': 'elon@example.com' +}); +// true +``` + ## Usage [Documentation](https://mrijk.github.io/speculaas) is in progress.