Skip to content

Commit

Permalink
Add asMatching
Browse files Browse the repository at this point in the history
  • Loading branch information
sz-piotr committed Jul 2, 2019
1 parent 194c325 commit f8ef386
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ asBoolean('boo') // FAIL (not a boolean)
asBoolean(123) // FAIL (not a boolean)
```

### `asMatching`

This higher-order sanitizer accepts values that are strings matching the regex provided as an argument. You can pass a custom message to it.

```javascript
const sanitizer = asMatching(/aaa/, 'custom message')

sanitizer('aaa') // OK 'aaa'
sanitizer(123) // FAIL (custom message)
sanitizer('b') // FAIL (custom message)
```

### `asObject`

This higher-order sanitizer requires a schema in the form of an object. Values of the schema are sanitizers used to sanitize the values of the input. Returns an object with keys and values matching the schema.
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@restless/restless",
"version": "0.2.1",
"version": "0.2.2",
"author": "Piotr Szlachciak <[email protected]>",
"description": "Express.js api, validations and more",
"keywords": [
"express",
"express",
"rest",
"api",
"validation",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export { asObject } from './sanitize/asObject'
export { asArray } from './sanitize/asArray'
export { asOptional } from './sanitize/asOptional'
export { asChecked } from './sanitize/asChecked'
export { asMatching } from './sanitize/asMatching'
6 changes: 6 additions & 0 deletions src/sanitize/asMatching.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Either, Sanitizer } from './sanitizer'

export const asMatching = (re: RegExp, message?: string): Sanitizer<string> =>
(value, path) => typeof value === 'string' && re.test(value)
? Either.right(value)
: Either.left([{ path, expected: message || `string matching ${re}` }])
8 changes: 4 additions & 4 deletions src/sanitize/asString.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Either, Sanitizer } from './sanitizer'

export const asString: Sanitizer<string> = (value, path) => {
if (typeof value === 'string') return Either.right(value)
else return Either.left([{ path, expected: 'string' }])
}
export const asString: Sanitizer<string> = (value, path) =>
typeof value === 'string'
? Either.right(value)
: Either.left([{ path, expected: 'string' }])
35 changes: 35 additions & 0 deletions test/sanitize/asMatching.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expect } from 'chai'
import { Either } from '../../src/sanitize/sanitizer'
import { asMatching } from '../../src/sanitize/asMatching'

describe('asString', () => {
it('sanitizes strings', async () => {
const asMyString = asMatching(/.*/)
const result = asMyString('hello', '')
expect(result).to.deep.equal(Either.right('hello'))
})

it('does not accept non-strings', async () => {
const asMyString = asMatching(/.*/)
const result = asMyString(123, 'path')
expect(result).to.deep.equal(
Either.left([{ path: 'path', expected: 'string matching /.*/' }])
)
})

it('checks the regex', async () => {
const asMyString = asMatching(/^a?b$/)
const result = asMyString('ac', 'path')
expect(result).to.deep.equal(
Either.left([{ path: 'path', expected: 'string matching /^a?b$/' }])
)
})

it('accepts a custom message', async () => {
const asMyString = asMatching(/^a?b$/, 'foo')
const result = asMyString('ac', 'path')
expect(result).to.deep.equal(
Either.left([{ path: 'path', expected: 'foo' }])
)
})
})

0 comments on commit f8ef386

Please sign in to comment.