valleyed 0.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/README.md +257 -0
- package/lib/index.js +24 -0
- package/package.json +61 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
|
+
|
|
5
|
+
## [0.0.0-alpha.0](https://github.com/kevinand11/valleyed/compare/v4.0.0-alpha.0...v0.0.0-alpha.0) (2023-02-14)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* lib src folder issue ([c30c2a4](https://github.com/kevinand11/valleyed/commit/c30c2a4a7eec944ad9f3ce1dc29f3614b991d548))
|
package/README.md
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
# Valleyed
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
## Installation
|
|
5
|
+
|
|
6
|
+
This is a [Node.js](https://nodejs.org/en/) module available through the [npm registry](https://www.npmjs.com/package/valleyed).
|
|
7
|
+
Before installing, [download and install Node.js](https://nodejs.org/en/download/). Node.js 4.2.4 or higher is required.
|
|
8
|
+
If this is a brand new project, make sure to create a `package.json` first with the [`npm init` command](https://docs.npmjs.com/creating-a-package-json-file).
|
|
9
|
+
Installation is done using the [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
|
|
10
|
+
|
|
11
|
+
### Using npm:
|
|
12
|
+
|
|
13
|
+
npm install valleyed
|
|
14
|
+
|
|
15
|
+
### Using yarn:
|
|
16
|
+
|
|
17
|
+
yarn add valleyed
|
|
18
|
+
|
|
19
|
+
### Using CDN:
|
|
20
|
+
|
|
21
|
+
[Validate jsDelivr CDN](https://www.jsdelivr.com/package/npm/valleyed)
|
|
22
|
+
|
|
23
|
+
### Using Skypack:
|
|
24
|
+
|
|
25
|
+
[Validate Skypack CDN](https://www.skypack.dev/view/valleyed)
|
|
26
|
+
|
|
27
|
+
## Contributing
|
|
28
|
+
|
|
29
|
+
[Contributing Guide](Contributing.md)
|
|
30
|
+
|
|
31
|
+
[](https://gitpod.io/#https://github.com/valleyed)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
## Basic Usage
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { isEmail, isMinOf } from 'valleyed'
|
|
38
|
+
|
|
39
|
+
// The isEmail function builds and returns a function that checks if the first argument is a valid email
|
|
40
|
+
let validity = isEmail()('johndoe@mail.co')
|
|
41
|
+
console.log(validity)
|
|
42
|
+
// Output should be : { valid: true, error: null, value: 'johndoe@mail.co' }
|
|
43
|
+
|
|
44
|
+
// The isMinOf function builds a function that checks if the first argument is of minimum length of the length passed into the builder function
|
|
45
|
+
validity = isMinOf(5)('abcd')
|
|
46
|
+
console.log(validity)
|
|
47
|
+
// Output should be : { valid: false, error: 'must contain 5 or more characters', value: 'abcd' }
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
## Common Rule Builders
|
|
52
|
+
|
|
53
|
+
### Errors
|
|
54
|
+
|
|
55
|
+
All rule builders accept an optional string as the last argument used to customize the error message
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
const res = isEmail('is missing specific characters')('')
|
|
59
|
+
console.log(res) // { valid: false, error: 'is missing specific characters', value: '' }
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Equality
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
// Checks if the validation value is shallow equal to the compare. Valuable for comparing primitive types
|
|
66
|
+
isShallowEqualTo(compare)
|
|
67
|
+
|
|
68
|
+
// Checks if the validation value resolves to true when passed into the compareFunction. The compareFunction passes the validation value and the compare value as the arguments and expects a boolean in return. Valuable for comparing non-primitive types.
|
|
69
|
+
isDeepEqualTo(compare, compareFunction)
|
|
70
|
+
const res = isDeepEqualTo({ id: 1 }, (value, compare) => {
|
|
71
|
+
return value?.id === compare.id
|
|
72
|
+
})({ id: 1 }) // res.valid is true
|
|
73
|
+
|
|
74
|
+
// Checks if the validation value is in an array of predefined values
|
|
75
|
+
arrayContains(array, compareFunction)
|
|
76
|
+
const res = arrayContains([{ id: 1 }, { id: 2 }], (value, compare) => {
|
|
77
|
+
return value?.id === compare.id
|
|
78
|
+
})({ id: 2 }) // res.valid is true
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Strings
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
// Checks if the validation value is of type String. This is used internally in all string methods, so no need to use it unless you are making a custom rule
|
|
85
|
+
isString()
|
|
86
|
+
|
|
87
|
+
// Checks if the length of the validation value is equal to the length
|
|
88
|
+
isLengthOf(length)
|
|
89
|
+
|
|
90
|
+
// Checks if the length of the validation value is greater than or equal to the length
|
|
91
|
+
isMinOf(length)
|
|
92
|
+
|
|
93
|
+
// Checks if the length of the validation value is less than or equal to the length
|
|
94
|
+
isMaxOf(length)
|
|
95
|
+
|
|
96
|
+
// Checks if the validation value is formatted as a valid email
|
|
97
|
+
isEmail()
|
|
98
|
+
|
|
99
|
+
// Checks if the validation value is formatted as a valid url
|
|
100
|
+
isUrl()
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Numbers
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
// Checks if the validation value is of type Number. This is used internally in all number methods, so no need to use it unless you are making a custom rule
|
|
107
|
+
isNumber()
|
|
108
|
+
|
|
109
|
+
// Checks if the the validation value is greater than the compare
|
|
110
|
+
isMoreThan(compare)
|
|
111
|
+
|
|
112
|
+
// Checks if the the validation value is greater than or equal to the compare
|
|
113
|
+
isMoreThanOrEqualTo(compare)
|
|
114
|
+
|
|
115
|
+
// Checks if the the validation value is less than the compare
|
|
116
|
+
isLessThan(compare)
|
|
117
|
+
|
|
118
|
+
// Checks if the the validation value is less than or equal to the compare
|
|
119
|
+
isLessThanOrEqualTo(compare)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Arrays
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
// Checks if the validation value is of type Array. This is used internally in all array methods, so no need to use it unless you are making a custom rule
|
|
126
|
+
isArray()
|
|
127
|
+
|
|
128
|
+
// Checks if the length of the validation value is equal to the length
|
|
129
|
+
hasLengthOf(length)
|
|
130
|
+
|
|
131
|
+
// Checks if the length of the validation value is greater than or equal to the length
|
|
132
|
+
hasMinOf(length)
|
|
133
|
+
|
|
134
|
+
// Checks if the length of the validation value is less than or equal to the length
|
|
135
|
+
hasMaxOf(length)
|
|
136
|
+
|
|
137
|
+
// Checks if the validation value is formatted as a valid email
|
|
138
|
+
isEmail()
|
|
139
|
+
|
|
140
|
+
// Checks if all elements in the valition value passes a requirement. The compare function passes a element and its index as the arguments and expects a boolean in return.
|
|
141
|
+
isArrayOf(compareFunction)
|
|
142
|
+
const res = isArrayOf((element, index) => {
|
|
143
|
+
return isString(element).valid // This ensures all elements in the array are strings
|
|
144
|
+
})(['a', 'b', 'c']) // res.valid is true
|
|
145
|
+
|
|
146
|
+
// Used to validate tuples(arrays that can contain different data types). Checks if all elements in the validation values passes a different requirement
|
|
147
|
+
isTuple(compareFunctionsArray)
|
|
148
|
+
const res = isTuple([
|
|
149
|
+
(element, index) => isString(element).valid,
|
|
150
|
+
(element, index) => isNumber(element).valid
|
|
151
|
+
])(['hello world', 2]) // res.valid is true because it expects an array that contains a string at index 0 and a number at index 1
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Datetime
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
// Checks if the validation value can be parsed into a valid javascript date. Validation value can be a Date object, a timestamp number or a datetime string. This is used internally in all datetime methods, so no need to use it unless you are making a custom rule
|
|
158
|
+
isTime()
|
|
159
|
+
|
|
160
|
+
// Checks if the validation value is later than the compare. Compare can also be a Date object, a timestamp number or a datetime string
|
|
161
|
+
isLaterThan(compare)
|
|
162
|
+
|
|
163
|
+
// Checks if the validation value is earlier than the compare. Compare can also be a Date object, a timestamp number or a datetime string
|
|
164
|
+
isEarlierThan(compare)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Other Types
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
// Checks if the validation value is a boolean
|
|
171
|
+
isBoolean()
|
|
172
|
+
|
|
173
|
+
// Checks if the validation value is null
|
|
174
|
+
isNull()
|
|
175
|
+
|
|
176
|
+
// Checks if the validation value is undefined
|
|
177
|
+
isUndefined()
|
|
178
|
+
|
|
179
|
+
// Checks if the validation value is an instance of the Class passed in
|
|
180
|
+
isInstanceOf(classDefinition)
|
|
181
|
+
const res = isInstanceOf(String)('') // res.valid is true
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Records and Maps
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
/// Checks if all values in an object passes a comparer function
|
|
188
|
+
isRecord(compareFunction)
|
|
189
|
+
const res = isRecord((currentValue) => {
|
|
190
|
+
return isNumber(currentValue).valid
|
|
191
|
+
})({ a: 1, b: 2, c: 3 }) // res.valid is true because all the values of the object are numbers
|
|
192
|
+
|
|
193
|
+
/// Checks if all keys and values in an map passes a comparer function
|
|
194
|
+
isMap(keysCompareFunction, valuesCompareFunction)
|
|
195
|
+
const map = new Map([
|
|
196
|
+
[1, true],
|
|
197
|
+
[2, false],
|
|
198
|
+
[3, true]
|
|
199
|
+
])
|
|
200
|
+
const res = isMap(
|
|
201
|
+
(key) => isNumber(key).valid,
|
|
202
|
+
(value) => isBoolean(value).valid
|
|
203
|
+
)(map) // res.valid is true because all the keys of the map are numbers and all the values are booleans
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Files
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
// Checks if the validation value is an object with a key "type" that contains a supported file mimetypes. All supported file mimetypes can be imported under the name "fileMimeTypes". This is used internally in all file methods, so no need to use it unless you are making a custom rule
|
|
210
|
+
isFile()
|
|
211
|
+
|
|
212
|
+
// Checks if the validation value's type is a valid image mimetype. All supported image mimetypes can be imported under the name "imageMimeTypes".
|
|
213
|
+
isImage()
|
|
214
|
+
|
|
215
|
+
// Checks if the validation value's type is a valid audio mimetype. All supported audio mimetypes can be imported under the name "audioMimeTypes".
|
|
216
|
+
isAudio()
|
|
217
|
+
|
|
218
|
+
// Checks if the validation value's type is a valid video mimetype. All supported video mimetypes can be imported under the name "videoMimeTypes".
|
|
219
|
+
isVideo()
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Custom Rule
|
|
223
|
+
|
|
224
|
+
```ts
|
|
225
|
+
// If there is a rule for your usecase, you can create a custom one with this. The validityFunction passes the validation value as its argument and expects a boolean in return
|
|
226
|
+
isCustom(validityFunction)
|
|
227
|
+
const res = isCustom((value) => typeof value === 'function')(() => {}) // res.valid is true because the typeof value is function
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
## Combining Rules
|
|
232
|
+
|
|
233
|
+
```ts
|
|
234
|
+
import { Validator, isEmail, isMinOf, isString, isNumber } from 'valleyed'
|
|
235
|
+
|
|
236
|
+
// The Validator.and function is used to build up a schema or list of rules to validate a value against
|
|
237
|
+
let res = Validator.and('johndoe@mail.com', [[isEmail(), isMinOf(1)]])
|
|
238
|
+
console.log(res) // { valid: true, errors: [], value: 'johndoe@mail.com' }
|
|
239
|
+
|
|
240
|
+
// if the value fails validation, it returns a list of all errors in the errors array
|
|
241
|
+
res = Validator.and('', [[isEmail(), isMinOf(1)]])
|
|
242
|
+
console.log(res) // { valid: false, value: '', errors: [ 'is not a valid email', 'must contain 1 or more characters' ] }
|
|
243
|
+
|
|
244
|
+
// Similar to the And function, Validator has an or function that checks if the value passes validation for any of the list of rules passed in
|
|
245
|
+
let res = Validator.or(2, [[isString(), isMinOf(1)], [isNumber()]])
|
|
246
|
+
console.log(res) // { valid: true, value: 2, errors: [] }
|
|
247
|
+
|
|
248
|
+
// if the value fails validation, it returns a list of all errors in the errors array
|
|
249
|
+
res = Validator.or(false, [[isString(), isMinOf(1)], [isNumber()]])
|
|
250
|
+
console.log(res) // { valid: false, value: false, errors: [ 'doesn't match any of the schema' ] }
|
|
251
|
+
|
|
252
|
+
// An optional third paramater can be passed into the And/Or functions to control if null and undefined are allowed to pass validation
|
|
253
|
+
Validator.and('', [[isEmail()]], {
|
|
254
|
+
nullable: true, // Boolean: if true, null passed as the first argument passes validation
|
|
255
|
+
required: () => false // Boolean or Function that returns a boolean: if false, undefined passed as the first argument passes validation
|
|
256
|
+
})
|
|
257
|
+
```
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.Validator = void 0;
|
|
18
|
+
__exportStar(require("./api"), exports);
|
|
19
|
+
var validators_1 = require("./validators");
|
|
20
|
+
Object.defineProperty(exports, "Validator", { enumerable: true, get: function () { return validators_1.Validator; } });
|
|
21
|
+
__exportStar(require("./utils/rules"), exports);
|
|
22
|
+
__exportStar(require("./rules"), exports);
|
|
23
|
+
__exportStar(require("./utils/functions"), exports);
|
|
24
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "valleyed",
|
|
3
|
+
"version": "0.0.0-alpha.0",
|
|
4
|
+
"description": "A lightweight package with definitions for various validation rules, and helper services to consume said rules.",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc --project tsconfig.build.json",
|
|
9
|
+
"lint": "eslint src/ --ext .ts --fix",
|
|
10
|
+
"test": "jest",
|
|
11
|
+
"release": "standard-version",
|
|
12
|
+
"prepare": "husky install"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"typescript",
|
|
16
|
+
"schema",
|
|
17
|
+
"validation",
|
|
18
|
+
"type",
|
|
19
|
+
"inference"
|
|
20
|
+
],
|
|
21
|
+
"author": "Kevin Izuchukwu",
|
|
22
|
+
"license": "ISC",
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@commitlint/cli": "^17.4.3",
|
|
25
|
+
"@commitlint/config-conventional": "^17.4.3",
|
|
26
|
+
"@types/jest": "^29.4.0",
|
|
27
|
+
"@types/node": "^18.13.0",
|
|
28
|
+
"@typescript-eslint/eslint-plugin": "^5.52.0",
|
|
29
|
+
"@typescript-eslint/parser": "^5.52.0",
|
|
30
|
+
"eslint": "^8.34.0",
|
|
31
|
+
"eslint-plugin-jest": "^27.2.1",
|
|
32
|
+
"eslint-plugin-promise": "^6.1.1",
|
|
33
|
+
"husky": "^8.0.3",
|
|
34
|
+
"jest": "^29.4.2",
|
|
35
|
+
"standard-version": "^9.5.0",
|
|
36
|
+
"ts-jest": "^29.0.5",
|
|
37
|
+
"typescript": "^4.9.5"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"LICENSE",
|
|
41
|
+
"CHANGELOG.md",
|
|
42
|
+
"README.md",
|
|
43
|
+
"bin",
|
|
44
|
+
"lib/src"
|
|
45
|
+
],
|
|
46
|
+
"repository": {
|
|
47
|
+
"url": "git://github.com/kevinand11/valleyed.git"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"registry": "https://registry.npmjs.org/"
|
|
51
|
+
},
|
|
52
|
+
"commitlint": {
|
|
53
|
+
"extends": [
|
|
54
|
+
"@commitlint/config-conventional"
|
|
55
|
+
]
|
|
56
|
+
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"url-regex-safe": "^3.0.0"
|
|
59
|
+
},
|
|
60
|
+
"standard-version": {}
|
|
61
|
+
}
|