Skip to content

Commit

Permalink
Merge pull request #1 from upfrontIO/string-pattern
Browse files Browse the repository at this point in the history
Allow to add regex patterns
  • Loading branch information
peyerluk authored Mar 21, 2018
2 parents b43f43b + e541df2 commit 2198223
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
201 changes: 187 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,205 @@
# microschema

<p align="center">
<p align="right">
<a href="https://travis-ci.org/upfrontIO/microschema">
<img alt="Travis" src="https://img.shields.io/travis/upfrontIO/microschema/master.svg">
</a>
<a href="https://semantic-release.gitbooks.io/semantic-release/content/#highlights">
<img alt="semantic-release" src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg">
</a>
<a href="https://www.npmjs.com/package/microschema">
<img alt="npm latest version" src="https://img.shields.io/npm/v/microschema/latest.svg">
</a>
<a href="https://semantic-release.gitbooks.io/semantic-release/content/#highlights">
<img alt="semantic-release" src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg">
</a>
</p>

# microschema

Helper library to create JSON Schemas in a concise way.
Small library without dependencies to create JSON Schemas in a concise way.

Example:
```js
const microschema = require('microschema')
const ms = require('microschema')

microschema.strictObj({
identity_id: 'string:required',
client_id: 'number',
redirect_uri: 'string:uri',
ms.strictObj({
identityId: 'string:required',
clientId: 'number',
redirectUri: 'string:uri',
scope: 'string',
ip_address: 'string',
children: microschema.arrayOf(microschema.strictObj({
ipAddress: ms.string({pattern: ''}),
children: ms.arrayOf(ms.strictObj({
scope: 'string'
}))
})
```


## Strings

Using the `ms.string()` method:
```js
ms.string()
output = {type: 'string'})
```

```js
ms.string({pattern: '[a-z]+'})

// Passing a javascript RegExp is equivalent to the above
ms.string({pattern: /[a-z]+/})

output = {
type: 'string',
pattern: '[a-z]+'
}
```

Setting the required flag (only possible within an object):
```js
ms.obj({
foo: ms.required.string()
})

output = {
type: 'object',
required: ['foo'],
properties: {
foo: {
type: 'string'
}
}
}
```

Simplified usage within objects:
```js
ms.obj({
foo: 'string'
})

output = {
type: 'object',
properties: {
foo: {
type: 'string'
}
}
}
```

```js
ms.obj({
foo: 'string:required'
})

output = {
type: 'object',
required: ['foo'],
properties: {
foo: {
type: 'string'
}
}
}
```

## Numbers and Integers

Simplified usage within objects:
```js
ms.obj({
foo: 'string'
})
```

Using the `ms.number()` method:
```js
ms.number()
output = {type: 'number'}
```

```js
ms.number({min: 0, max: 10})

output = {
type: 'number',
minimum: 0,
maximum: 10
}
```

Using the `ms.integer()` method:
```js
ms.integer()
output = {type: 'integer'}
```

The `integer()` methods also accepts `min` and `max` params the same as `number()` does.


## Booleans

```js
ms.boolean()
output = {type: 'boolean'})
```

Simplified usage within objects:
```js
ms.obj({
foo: 'boolean:required'
})

output = {
type: 'object',
required: ['foo'],
properties: {
foo: {
type: 'boolean'
}
}
}
```

## Objects

```js
ms.obj()
output = {type: 'object'}
```

Don't allow additional properties with `strictObj()`:
```js
ms.strictObj({
count: ms.integer()
})

output = {
type: 'object',
additionalProperties: false,
properties: {
count: {type: 'integer'}
}
}
```

## Arrays

```js
ms.arrayOf(ms.string())

output = {
type: 'array',
items: {type: 'string'}
}
```


## Enumerations

```js
// All values in an enumeration must be of the same type.
ms.enum('foo', 'bar')

output = {
type: 'string',
enum: ['foo', 'bar']
}
```
19 changes: 19 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ module.exports = {
})
},

string ({pattern} = {}) {
const s = {type: 'string'}
if (pattern) {
if (pattern instanceof RegExp) {
if (pattern.flags) {
throw new Error(`JSON schema does not support regexp flags: ${pattern}`)
}
s.pattern = pattern.source
} else {
s.pattern = pattern
}
}
return this.decorate(s)
},

number ({min, max, integer} = {}) {
const type = integer ? 'integer' : 'number'
const s = {type: type}
Expand All @@ -115,6 +130,10 @@ module.exports = {
return this.number(opts)
},

boolean () {
return this.decorate({type: 'boolean'})
},

decorate (obj) {
if (this[isRequired]) obj[isRequired] = true
return obj
Expand Down
Loading

0 comments on commit 2198223

Please sign in to comment.