Skip to content

Commit

Permalink
Remove extra
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexGalays committed Apr 16, 2021
1 parent 9d6eee5 commit 1be1c9f
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 334 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ node_modules
commonjs
es
test/test.js
test/extra.js
*.log
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const bigArray = array(string).and(minSize(100))
bigArray.validate(['1', '2']).ok // false
```

Note: the extra `minSize` validator does exactly that, but for more input types.
Note: the `minSize` validator does exactly that, but for more input types.

If you need to start from any value, you can use the `unknown` validator that always succeeds.

Expand Down Expand Up @@ -158,7 +158,7 @@ import {
intFromString,
minSize,
nonEmpty
} from 'idonttrustlikethat/extra'
} from 'idonttrustlikethat'
```

And all the types:
Expand Down Expand Up @@ -516,15 +516,15 @@ Ensures an Array, Object, string, Map or Set has a minimum size. You can also us

```ts
import {dictionary, string} from 'idonttrustlikethat'
import {minSize} from 'idonttrustlikethat/extra'
import {minSize} from 'idonttrustlikethat'

const dictionaryWithAtLeast10Items = dictionary(string, string).and(minSize(10))
```

### isoDate

```ts
import { isoDate } from 'idonttrustlikethat/extra'
import { isoDate } from 'idonttrustlikethat'

isoDate.validate('2011-10-05T14:48:00.000Z').ok // true
```
Expand All @@ -534,7 +534,7 @@ isoDate.validate('2011-10-05T14:48:00.000Z').ok // true
Validates that a string is a valid URL, and returns that string.

```ts
import { url, absoluteUrl, relativeUrl } from 'idonttrustlikethat/extra'
import { url, absoluteUrl, relativeUrl } from 'idonttrustlikethat'

absoluteUrl.validate('https://ebay.com').ok // true
```
Expand All @@ -544,7 +544,7 @@ absoluteUrl.validate('https://ebay.com').ok // true
Validates that a string encodes a boolean and returns the boolean.

```ts
import { booleanFromString } from 'idonttrustlikethat/extra'
import { booleanFromString } from 'idonttrustlikethat'

booleanFromString.validate('true').ok // true
```
Expand All @@ -554,7 +554,7 @@ booleanFromString.validate('true').ok // true
Validates that a string encodes a number (float or integer) and returns the number.

```ts
import { numberFromString } from 'idonttrustlikethat/extra'
import { numberFromString } from 'idonttrustlikethat'

numberFromString.validate('123.4').ok // true
```
Expand All @@ -564,7 +564,7 @@ numberFromString.validate('123.4').ok // true
Validates that a string encodes an integer and returns the number.

```ts
import { intFromString } from 'idonttrustlikethat/extra'
import { intFromString } from 'idonttrustlikethat'

intFromString.validate('123').ok // true
```
Expand All @@ -578,7 +578,7 @@ A Configuration object can be passed to modify the default behavior of the valid
Transforms every keys of every objects before validating.

```ts
import {snakeCaseTransformation} from 'idonttrustlikethat/extra'
import {snakeCaseTransformation} from 'idonttrustlikethat'

const burger = v.object({
options: v.object({
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "idonttrustlikethat",
"version": "2.0.0",
"version": "2.0.1",
"sideEffects": false,
"description": "Validation for TypeScript",
"license": "MIT",
"main": "commonjs/core.js",
"module": "es/core.js",
"typings": "commonjs/core.d.ts",
"main": "commonjs/validation.js",
"module": "es/validation.js",
"typings": "commonjs/validation.d.ts",
"devDependencies": {
"chalk": "1.1.1",
"cross-env": "5.2.0",
Expand All @@ -17,9 +17,9 @@
},
"scripts": {
"build": "npm run build-es && npm run build-commonjs",
"build-es": "tsc src/core.ts src/extra.ts --outDir es --strict --noUnusedParameters --declaration --lib dom,es5,es6 --module es6 --target es6 --moduleResolution node",
"build-commonjs": "tsc src/core.ts src/extra.ts --outDir commonjs --strict --noUnusedParameters --declaration --lib dom,es5,es6 --target es5",
"pretest": "npm run build && tsc test/mocha.d.ts test/expect.d.ts test/test.ts test/extra.ts --lib dom,es5,es6 --strict",
"build-es": "tsc src/validation.ts --outDir es --strict --noUnusedParameters --declaration --lib dom,es5,es6 --module es6 --target es6 --moduleResolution node",
"build-commonjs": "tsc src/validation.ts --outDir commonjs --strict --noUnusedParameters --declaration --lib dom,es5,es6 --target es5",
"pretest": "npm run build && tsc test/mocha.d.ts test/expect.d.ts test/test.ts --lib dom,es5,es6 --strict",
"test": "mocha --recursive && node test/testCompilationErrors.js",
"locale-test": "cross-env LANG=tr_TR npm run test"
},
Expand Down
108 changes: 0 additions & 108 deletions src/extra.ts

This file was deleted.

107 changes: 107 additions & 0 deletions src/core.ts → src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,110 @@ export function errorDebugString(errors: ValidationError[]) {
//--------------------------------------

export { nullValidator as null, undefinedValidator as undefined }

//--------------------------------------
// extra validators
//--------------------------------------

export function recursion<T>(
definition: (self: Validator<T>) => Validator<unknown>
): Validator<T> {
const Self = new Validator<T>((value, config, path) =>
Result.validate(value, config, path)
)
const Result: any = definition(Self)
return Result
}

export const isoDate = string.and(str => {
const date = new Date(str)
return isNaN(date.getTime())
? Err(`Expected ISO date, got: ${prettifyJson(str)}`)
: Ok(date)
})

//--------------------------------------
// config
//--------------------------------------

const upperThenLower = /([A-Z]+)([A-Z][a-z])/g
const lowerThenUpper = /([a-z\\\\d])([A-Z])/g
export const snakeCaseTransformation = (key: string): string =>
key
.replace(upperThenLower, '$1_$2')
.replace(lowerThenUpper, '$1_$2')
.toLowerCase()

//--------------------------------------
// url
//--------------------------------------

export const relativeUrl = (baseUrl: string = 'http://some-domain.com') =>
string.and(str => {
try {
new URL(str, baseUrl)
return Ok(str)
} catch (err) {
return Err(`${str} is not a relative URL for baseURL: ${baseUrl}`)
}
})

export const absoluteUrl = string.and(str => {
try {
new URL(str)
return Ok(str)
} catch (err) {
return Err(`${str} is not an absolute URL`)
}
})

export const url = union(absoluteUrl, relativeUrl())

//--------------------------------------
// parsed from string
//--------------------------------------

export const booleanFromString = union('true', 'false')
.withError(v => `Expected "true" | "false", got: ${v}`)
.map(str => str === 'true')

export const numberFromString = string.and(str => {
const parsed = Number(str)
return Number.isNaN(parsed)
? Err(`"${str}" is not a stringified number`)
: Ok(parsed)
})

export const intFromString = numberFromString.and(num => {
return Number.isInteger(num) ? Ok(num) : Err(`${num} is not an int`)
})

//--------------------------------------
// generic refinement functions
//--------------------------------------

type HasSize =
| object
| string
| Array<unknown>
| Map<unknown, unknown>
| Set<unknown>

export function minSize<T extends HasSize>(minSize: number) {
return (value: T) => {
const size =
typeof value === 'string'
? value.length
: Array.isArray(value)
? value.length
: value instanceof Map || value instanceof Set
? value.size
: Object.keys(value).length

return size >= minSize
? Ok(value)
: Err(`Expected a min size of ${minSize}, got ${size}`)
}
}

export const nonEmpty = minSize(1)
Loading

0 comments on commit 1be1c9f

Please sign in to comment.