Skip to content
This repository was archived by the owner on Jun 6, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 44 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# [@tinkerstorm/validate](https://npmjs.org/package/@tinkerstorm/validate)
# @tinkerstorm/validate

![CI status](https://github.com/TinkerStorm/validate/workflows/CI/badge.svg)
![NPM build](https://badgen.net/npm/v/@tinkerstorm/validate)
![NPM types](https://badgen.net/npm/types/@tinkerstorm/validate)
![GitHub license](https://badgen.net/github/license/TinkerStorm/validate)
![GitHub stars](https://badgen.net/github/stars/TinkerStorm/validate)
[![npm (version)](https://badgen.net/npm/v/@tinkerstorm/validate)](https://npm.im/@tinkerstorm/validate)
[![npm (types)](https://badgen.net/npm/types/@tinkerstorm/validate)](https://npm.im/@tinkerstorm/validate)
[![npm (downloads)](https://badgen.net/npm/dw/@tinkerstorm/validate)](https://npm.im/@tinkerstorm/validate)
[![npm (dependants)](https://badgen.net/npm/dependents/@tinkerstorm/validate)](https://npm.im/@tinkerstorm/validate)

## Open Issues
[![github (workflow)](https://github.com/TinkerStorm/validate/workflows/Build/badge.svg)](https://github.com/TinkerStorm/validate/actions)
[![github (stars)](https://badgen.net/github/stars/TinkerStorm/validate)](https://github.com/TinkerStorm/validate/stargazers)
[![github (open-issues)](https://badgen.net/github/open-issues/TinkerStorm/validate)](https://github.com/TinkerStorm/validate/issues)
[![github (open-prs)](https://badgen.net/github/open-prs/TinkerStorm/validate)](https://github.com/TinkerStorm/validate/pulls)

- [#4 - Thrown error scope](https://github.com/TinkerStorm/validate/issues/4)
[![linter (xo)](https://badgen.net/badge/🎨%20linter/xo/690DCA)](https://github.com/xojs/xo)
[![test env (ava)](https://badgen.net/badge/🧰%20toolbox/ava/F84824)](https://github.com/avajs/ava)
[![discord (chat)](https://badgen.net/badge/discord/chat/7289DA?icon=discord)](https://discord.gg/Bb3JQQG)

## API

Expand All @@ -21,20 +25,21 @@ Where the function is called from (human-readable string).
#### args

A list of arguments that define how the function accepts parameters.
> The argument is to be passed as an array, which is then deconstructed into the positional descriptors below.

---

### ValidateOptions
### ValidateOptions as [name, value, types, options?]

| Argument | Type | Description |
| ------------------ | ------------------------ | ---------------------------------------------------------------------------- |
| `name` | string | Name of the argument as a string literal. |
| `value` | any | The value of the argument as anything that the `types` and `options` accept. |
| `types` | string, object, function | An array of type resolvers. |
| `options` | object? | Argument options, can be omitted. |
| `options.inverse` | boolean? | Flip the condition state. |
| `options.rest` | boolean? | Process the argument as an array. |
| `options.optional` | boolean? | Make the argument not throw if it's null / undefined. Short hand check. |
| Argument | Type | Description |
| ------------------ | -------------------------- | ---------------------------------------------------------------------------- |
| `name` | `string` | Name of the argument as a string literal. |
| `value` | `any` | The value of the argument as anything that the `types` and `options` accept. |
| `types` | `string, object, function` | An array of type resolvers. |
| `options` | `object?` | Argument options, can be omitted. |
| `options.inverse` | `boolean?` | Flip the condition state. |
| `options.rest` | `boolean?` | Process the argument as an array. |
| `options.optional` | `boolean?` | Make the argument not throw if it's null / undefined. Short hand check. |

## API (Future)

Expand All @@ -43,22 +48,29 @@ A list of arguments that define how the function accepts parameters.
- `validate.one(origin: string, name: string, value: any, types: (string | object | () => boolean)[], options?: ValidateOptions)`
- `validate.arg(types: (string | object | () => boolean)[], options?: ValidateOptions)`

---

- `validate.checks[key: string](value?: any) => boolean`
- `validate.checks.exists(value?: any) => boolean`
- `validate.checks.optional(value?: any) => boolean` - Possiblity to move `options.optional` to this predicate...
- `validate.checks.truthy(value?: any) => boolean`
- `validate.checks.int(value: int?) => boolean`

### Type bound

*This would make use of predicate construction by passing the context of the previous scope through to the child.*

- `validate.checks.min<T>(n: T) => (value: T?) => boolean`
- `validate.checks.max<T>(n: T) => (value: T?) => boolean`
- `validate.checks.between<T>(low: T, high: T) => (value: T?) => boolean`
> The generic of `T` would infer that the accepted type has already been checked or is yet to be checked, but will default to that of primative `number`.
> - Could also be applied to `string` or `Array` when making use of the `.length` property or `.valueOf()` method. Either would work.

## Why?

Why not? Some code is designed to look like it shouldn't exist, but does anyway and works just as well. It was originally designed to be a simple 'private' method that would sit in a class structure or module without the need to use it elsewhere.

```js
const typeofAny = (target, ...types) => types.some(type => typeof target === type);

// first attempt did not include the options parameter
function validate(origin, args) {
for (let [varName, value, anyof] of args) {
if (!typeofAny(value, ...anyof)) {
throw new TypeError(`${varName} (${value}) from ${origin} is not of any type ${anyof}`);
}
}
}
```
At the base level of functionality, it verifies the type declarations, then object derivatives / class structures, and finally functions. While it may not seem like much, the overarching functions with the array, inverse and optional support provide some functionality I had never intended to begin with.

At the end of it all, this first working version probably took around 3 or 4 hours (including testing and minor patches).

Expand All @@ -67,4 +79,5 @@ At the end of it all, this first working version probably took around 3 or 4 hou
While I do like what I've accomplished here, it is still a joke in many ways and there are people I'd like to thank for supporting what I do or those who I look up to as inspiration.

- [@Koltonix](https://github.com/Koltonix) and [@Mistiare](https://github.com/Mistiare) for their guidance and support.
- [@sindresorhus](https://github.com/sindresorhus) for continuing to provide the basis on how I, personally, should build my modules and projects (or at least a target to aim for).
- [@sindresorhus](https://github.com/sindresorhus) for continuing to provide the basis on how I, personally, should build my modules and projects (or at least a target to aim for).
> Also for the amazing toolset of [xo](https://github.com/xojs/xo) and [ava](https://avajs.dev).
8 changes: 8 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// Internal

/*
declare function error<Value>(origin: string, arg: string, value: Value, types: any[]): void
declare function signal<Value>(value: Value, type: any): boolean
declare function multi<Values>(values: Values, types: any[]): boolean
declare function check<Value>(value: Value, types: any[], options?: ValidateOptions): boolean
declare function condition<Value>(value: Value, result: boolean, options?: ValidateOptions): boolean
*/

// Exported

interface ValidateOptions {
Expand Down