validup 0.1.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/LICENSE +21 -0
- package/README.md +172 -0
- package/dist/constants.d.ts +3 -0
- package/dist/errors/attribute.d.ts +14 -0
- package/dist/errors/base.d.ts +2 -0
- package/dist/errors/index.d.ts +3 -0
- package/dist/errors/nested.d.ts +7 -0
- package/dist/helpers/array-to-property-path.d.ts +5 -0
- package/dist/helpers/error.d.ts +1 -0
- package/dist/helpers/expand-property-path.d.ts +1 -0
- package/dist/helpers/get-property-path-value.d.ts +2 -0
- package/dist/helpers/index.d.ts +4 -0
- package/dist/helpers/property-path-to-array.d.ts +7 -0
- package/dist/index.cjs +388 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.mjs +376 -0
- package/dist/index.mjs.map +1 -0
- package/dist/module.d.ts +10 -0
- package/dist/types.d.ts +35 -0
- package/dist/utils/has-own-property.d.ts +1 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/object.d.ts +1 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Peter Placzek
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# Validup 🛡️
|
|
2
|
+
|
|
3
|
+
[](https://github.com/tada5hi/validup/actions/workflows/main.yml)
|
|
4
|
+
[](https://github.com/tada5hi/validup/actions/workflows/codeql.yml)
|
|
5
|
+
[](https://snyk.io/test/github/tada5hi/validup)
|
|
6
|
+
[](https://conventionalcommits.org)
|
|
7
|
+
|
|
8
|
+
This is a library to create domain specific validators.
|
|
9
|
+
|
|
10
|
+
> 🚧 **Work in Progress**
|
|
11
|
+
>
|
|
12
|
+
> Validup is currently under active development and is not yet ready for production.
|
|
13
|
+
|
|
14
|
+
**Table of Contents**
|
|
15
|
+
|
|
16
|
+
- [Features](#features)
|
|
17
|
+
- [Installation](#installation)
|
|
18
|
+
- [Usage](#usage)
|
|
19
|
+
- [Concepts](#concepts)
|
|
20
|
+
- [Errors](#errors)
|
|
21
|
+
- [Mounting](#mounting)
|
|
22
|
+
- [Nesting](#nesting)
|
|
23
|
+
- [Groups](#groups)
|
|
24
|
+
- [API](#api)
|
|
25
|
+
- [License](#license)
|
|
26
|
+
|
|
27
|
+
## Features
|
|
28
|
+
|
|
29
|
+
- ✨ Simple API
|
|
30
|
+
- 🌐 Works in any (Node.Js, browser & workers) environment
|
|
31
|
+
- ❌ Dedicated errors for different scenarios
|
|
32
|
+
- 🚀 Nesting of Validators and Containers
|
|
33
|
+
- 🎭 Easy integrate third-party validators/sanitizers (zod, validator.js, ...)
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm install validup --save
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
A validator is an (async) function to validate (and modify) any piece of information.
|
|
44
|
+
Each validator receives a [context](#validatorcontext) containing the actual value, key (aka path), ...
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import {
|
|
48
|
+
Validator,
|
|
49
|
+
ValidatorContext,
|
|
50
|
+
ValidupValidatorError
|
|
51
|
+
} from 'validup';
|
|
52
|
+
|
|
53
|
+
const isString: Validator = (ctx: ValidatorContext) => {
|
|
54
|
+
if (typeof ctx.value !== 'string') {
|
|
55
|
+
throw new ValidupValidatorError({
|
|
56
|
+
message: `The validator for ${ctx.path} expected a string as input.`,
|
|
57
|
+
expected: 'string',
|
|
58
|
+
path: ctx.path
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return ctx.value;
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
This validator can than be mounted on a specific path to a container. A mount path can either be
|
|
67
|
+
a regular string or a glob pattern.
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { Container } from 'validup';
|
|
71
|
+
|
|
72
|
+
const container = new Container();
|
|
73
|
+
container.mount('foo', isString);
|
|
74
|
+
|
|
75
|
+
const output = await container.run({
|
|
76
|
+
foo: 'bar',
|
|
77
|
+
bar: 'baz'
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
console.log(output);
|
|
81
|
+
// { foo: 'bar' }
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
> [!NOTE]
|
|
85
|
+
> The container run method throws an error if an error occurred during the execution of a validator.
|
|
86
|
+
|
|
87
|
+
## Concepts
|
|
88
|
+
|
|
89
|
+
### Errors
|
|
90
|
+
|
|
91
|
+
During the execution of a [validator](#validator), a ValidupValidatorError or ValidupNestedError can be thrown.
|
|
92
|
+
A ValidupNestedError bundles multiple ValidupValidationErrors.
|
|
93
|
+
|
|
94
|
+
When the execution of a Container fails, the container will always throw a ValidupNestedError.
|
|
95
|
+
Unknown errors will be converted to a ValidupValidatorError.
|
|
96
|
+
|
|
97
|
+
### Mounting
|
|
98
|
+
|
|
99
|
+
> 🚧 **Work in Progress**
|
|
100
|
+
> ...
|
|
101
|
+
|
|
102
|
+
### Nesting
|
|
103
|
+
|
|
104
|
+
> 🚧 **Work in Progress**
|
|
105
|
+
>
|
|
106
|
+
|
|
107
|
+
### Groups
|
|
108
|
+
When mounting a validator or container, it is possible to restrict the execution to explicit execution groups.
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
import { Container } from 'validup';
|
|
112
|
+
|
|
113
|
+
const container = new Container();
|
|
114
|
+
container.mount('id', { group: ['update', 'delete'] }, isString);
|
|
115
|
+
container.mount('name', { group: ['craete', 'update', 'delete']}, isString);
|
|
116
|
+
|
|
117
|
+
const output = await container.run(
|
|
118
|
+
{
|
|
119
|
+
id: 'xxx',
|
|
120
|
+
name: 'foo'
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
group: 'create'
|
|
124
|
+
}
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
console.log(output);
|
|
128
|
+
// { name: 'foo' }
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
In this example the output will only contain the value of the key name.
|
|
132
|
+
The reason for this is, that the id key is not registered for the create group.
|
|
133
|
+
|
|
134
|
+
> [!NOTE]
|
|
135
|
+
> To always execute a mounted container/validator do not specify
|
|
136
|
+
> any group or use the `*` group.
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
## API
|
|
140
|
+
|
|
141
|
+
### Validator
|
|
142
|
+
```ts
|
|
143
|
+
type Validator = (ctx: ValidatorContext) => Promise<unknown> | unknown;
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### ValidatorContext
|
|
147
|
+
```ts
|
|
148
|
+
type ValidatorContext = {
|
|
149
|
+
/**
|
|
150
|
+
* Expanded mount path.
|
|
151
|
+
*/
|
|
152
|
+
key: string,
|
|
153
|
+
/**
|
|
154
|
+
* mount path (simple key or glob pattern)
|
|
155
|
+
*/
|
|
156
|
+
keyRaw: string,
|
|
157
|
+
/**
|
|
158
|
+
* The actual value.
|
|
159
|
+
*/
|
|
160
|
+
value: unknown,
|
|
161
|
+
/**
|
|
162
|
+
* Input data
|
|
163
|
+
*/
|
|
164
|
+
data: Record<string, any>
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## License
|
|
169
|
+
|
|
170
|
+
Made with 💚
|
|
171
|
+
|
|
172
|
+
Published under [MIT License](./LICENSE).
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type ValidupValidatorErrorOptions = {
|
|
2
|
+
path: string;
|
|
3
|
+
message?: string;
|
|
4
|
+
code?: string | null;
|
|
5
|
+
received?: unknown;
|
|
6
|
+
expected?: unknown;
|
|
7
|
+
};
|
|
8
|
+
export declare class ValidupValidatorError extends Error {
|
|
9
|
+
readonly path: string;
|
|
10
|
+
readonly code?: string | null;
|
|
11
|
+
readonly received?: unknown;
|
|
12
|
+
readonly expected?: unknown;
|
|
13
|
+
constructor(options: ValidupValidatorErrorOptions);
|
|
14
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ValidupValidatorError } from './attribute';
|
|
2
|
+
import { ValidupError } from './base';
|
|
3
|
+
export declare class ValidupNestedError extends ValidupError {
|
|
4
|
+
children: ValidupValidatorError[];
|
|
5
|
+
constructor(message?: string, children?: ValidupValidatorError[]);
|
|
6
|
+
addChild(child: ValidupValidatorError): void;
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function buildErrorMessageForAttributes(input: (number | string)[] | Record<string, any>): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function expandPropertyPath(data: Record<string, any>, path: string | string[], currPath: readonly string[]): string[];
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare function getPropertyPathValue(data: Record<string, any>, path: string): unknown;
|
|
2
|
+
export declare function setPropertyPathValue(data: Record<string, any> | Record<string, any>[], path: string | string[], value: unknown): Record<string, any> | Record<string, any>[];
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var smob = require('smob');
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* Copyright (c) 2024.
|
|
7
|
+
* Author Peter Placzek (tada5hi)
|
|
8
|
+
* For the full copyright and license information,
|
|
9
|
+
* view the LICENSE file that was distributed with this source code.
|
|
10
|
+
*/ /**
|
|
11
|
+
* @see https://github.com/express-validator/express-validator/blob/bec1dcbaa29002dcd21093ec84818c4671063b5d/src/field-selection.ts#L214
|
|
12
|
+
* @param parts
|
|
13
|
+
*/ function arrayToPropertyPath(parts) {
|
|
14
|
+
return parts.reduce((prev, segment)=>{
|
|
15
|
+
let part = '';
|
|
16
|
+
segment = segment === '\\*' ? '*' : segment;
|
|
17
|
+
// TODO: Handle brackets?
|
|
18
|
+
if (segment.includes('.')) {
|
|
19
|
+
// Special char key access
|
|
20
|
+
part = `["${segment}"]`;
|
|
21
|
+
} else if (/^\d+$/.test(segment)) {
|
|
22
|
+
// Index access
|
|
23
|
+
part = `[${segment}]`;
|
|
24
|
+
} else if (prev) {
|
|
25
|
+
// Object key access
|
|
26
|
+
part = `.${segment}`;
|
|
27
|
+
} else {
|
|
28
|
+
// Top level key
|
|
29
|
+
part = segment;
|
|
30
|
+
}
|
|
31
|
+
return prev + part;
|
|
32
|
+
}, '');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function buildErrorMessageForAttributes(input) {
|
|
36
|
+
let names;
|
|
37
|
+
if (Array.isArray(input)) {
|
|
38
|
+
names = smob.distinctArray(input);
|
|
39
|
+
} else {
|
|
40
|
+
names = Object.keys(input);
|
|
41
|
+
}
|
|
42
|
+
if (names.length === 0) {
|
|
43
|
+
return 'An unexpected error occurred.';
|
|
44
|
+
}
|
|
45
|
+
if (names.length > 1) {
|
|
46
|
+
return `The attributes ${names.join(', ')} are invalid.`;
|
|
47
|
+
}
|
|
48
|
+
return `The attribute ${String(names[0])} is invalid.`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/*
|
|
52
|
+
* Copyright (c) 2024.
|
|
53
|
+
* Author Peter Placzek (tada5hi)
|
|
54
|
+
* For the full copyright and license information,
|
|
55
|
+
* view the LICENSE file that was distributed with this source code.
|
|
56
|
+
*/ // eslint-disable-next-line @typescript-eslint/ban-types
|
|
57
|
+
function hasOwnProperty(obj, prop) {
|
|
58
|
+
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/*
|
|
62
|
+
* Copyright (c) 2024.
|
|
63
|
+
* Author Peter Placzek (tada5hi)
|
|
64
|
+
* For the full copyright and license information,
|
|
65
|
+
* view the LICENSE file that was distributed with this source code.
|
|
66
|
+
*/ function isObject(input) {
|
|
67
|
+
return !!input && typeof input === 'object' && !Array.isArray(input);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/*
|
|
71
|
+
* Copyright (c) 2024.
|
|
72
|
+
* Author Peter Placzek (tada5hi)
|
|
73
|
+
* For the full copyright and license information,
|
|
74
|
+
* view the LICENSE file that was distributed with this source code.
|
|
75
|
+
*/ /**
|
|
76
|
+
* Match property names within property paths.
|
|
77
|
+
*
|
|
78
|
+
* @see https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/lodash.js#L146C7-L146C118
|
|
79
|
+
*/ const rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g;
|
|
80
|
+
/**
|
|
81
|
+
* Match backslashes in property paths.
|
|
82
|
+
*
|
|
83
|
+
* @see https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/lodash.js#L168C1-L168C54
|
|
84
|
+
*/ const reEscapeChar = /\\(\\)?/g;
|
|
85
|
+
/**
|
|
86
|
+
* Convert string to property path array.
|
|
87
|
+
*
|
|
88
|
+
* @see https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/lodash.js#L6735
|
|
89
|
+
* @param segment
|
|
90
|
+
*/ function propertyPathToArray(segment) {
|
|
91
|
+
const result = [];
|
|
92
|
+
if (segment.charCodeAt(0) === 46 /* . */ ) {
|
|
93
|
+
result.push('');
|
|
94
|
+
}
|
|
95
|
+
segment.replace(rePropName, (match, number, quote, subString)=>{
|
|
96
|
+
result.push(quote ? subString.replace(reEscapeChar, '$1') : number || match);
|
|
97
|
+
return match;
|
|
98
|
+
});
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function getPropertyPathValue(data, path) {
|
|
103
|
+
const parts = propertyPathToArray(path);
|
|
104
|
+
let temp = data;
|
|
105
|
+
let index = 0;
|
|
106
|
+
while(index < parts.length){
|
|
107
|
+
if (!isObject(temp) && !Array.isArray(temp)) {
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
temp = temp[parts[index]];
|
|
111
|
+
index++;
|
|
112
|
+
}
|
|
113
|
+
return temp;
|
|
114
|
+
}
|
|
115
|
+
function isIndex(input) {
|
|
116
|
+
return typeof input === 'number' || !Number.isNaN(Number.parseInt(input, 10));
|
|
117
|
+
}
|
|
118
|
+
function setPropertyPathValue(data, path, value) {
|
|
119
|
+
const parts = Array.isArray(path) ? path : propertyPathToArray(path);
|
|
120
|
+
let temp = data;
|
|
121
|
+
let index = 0;
|
|
122
|
+
while(index < parts.length){
|
|
123
|
+
if (!Array.isArray(temp) && !isObject(temp)) {
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
const key = parts[index];
|
|
127
|
+
// [foo, '0']
|
|
128
|
+
if (typeof temp[key] === 'undefined') {
|
|
129
|
+
if (isIndex(key + 1)) {
|
|
130
|
+
temp[key] = [];
|
|
131
|
+
} else {
|
|
132
|
+
temp[key] = {};
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (index === parts.length - 1) {
|
|
136
|
+
temp[key] = value;
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
index++;
|
|
140
|
+
temp = temp[key];
|
|
141
|
+
}
|
|
142
|
+
return data;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/*
|
|
146
|
+
* Copyright (c) 2024.
|
|
147
|
+
* Author Peter Placzek (tada5hi)
|
|
148
|
+
* For the full copyright and license information,
|
|
149
|
+
* view the LICENSE file that was distributed with this source code.
|
|
150
|
+
*/ exports.GroupKey = void 0;
|
|
151
|
+
(function(GroupKey) {
|
|
152
|
+
GroupKey["WILDCARD"] = "*";
|
|
153
|
+
})(exports.GroupKey || (exports.GroupKey = {}));
|
|
154
|
+
|
|
155
|
+
/*
|
|
156
|
+
* Copyright (c) 2024-2024.
|
|
157
|
+
* Author Peter Placzek (tada5hi)
|
|
158
|
+
* For the full copyright and license information,
|
|
159
|
+
* view the LICENSE file that was distributed with this source code.
|
|
160
|
+
*/ class ValidupValidatorError extends Error {
|
|
161
|
+
constructor(options){
|
|
162
|
+
super(options.message);
|
|
163
|
+
this.path = options.path;
|
|
164
|
+
this.code = options.code;
|
|
165
|
+
this.received = options.received;
|
|
166
|
+
this.expected = options.expected;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/*
|
|
171
|
+
* Copyright (c) 2024-2024.
|
|
172
|
+
* Author Peter Placzek (tada5hi)
|
|
173
|
+
* For the full copyright and license information,
|
|
174
|
+
* view the LICENSE file that was distributed with this source code.
|
|
175
|
+
*/ class ValidupError extends Error {
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
class ValidupNestedError extends ValidupError {
|
|
179
|
+
addChild(child) {
|
|
180
|
+
this.children.push(child);
|
|
181
|
+
}
|
|
182
|
+
constructor(message, children){
|
|
183
|
+
super(message);
|
|
184
|
+
this.children = children || [];
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function expandPropertyPath(data, path, currPath) {
|
|
189
|
+
const segments = Array.isArray(path) ? path : propertyPathToArray(path);
|
|
190
|
+
if (!segments.length) {
|
|
191
|
+
// no more paths to traverse
|
|
192
|
+
return [
|
|
193
|
+
arrayToPropertyPath(currPath)
|
|
194
|
+
];
|
|
195
|
+
}
|
|
196
|
+
const key = segments[0];
|
|
197
|
+
const rest = segments.slice(1);
|
|
198
|
+
if (typeof data !== 'undefined' && data !== null && !isObject(data) && !Array.isArray(data)) {
|
|
199
|
+
if (key === '**' && !rest.length) {
|
|
200
|
+
// globstar leaves are always selected
|
|
201
|
+
return [
|
|
202
|
+
arrayToPropertyPath(currPath)
|
|
203
|
+
];
|
|
204
|
+
}
|
|
205
|
+
// there still are paths to traverse, but value is a primitive, stop
|
|
206
|
+
return [];
|
|
207
|
+
}
|
|
208
|
+
// Use a non-null value so that non-existing fields are still selected
|
|
209
|
+
data = data || {};
|
|
210
|
+
if (key === '*') {
|
|
211
|
+
return Object.keys(data).flatMap((key)=>expandPropertyPath(data[key], rest, currPath.concat(key)));
|
|
212
|
+
}
|
|
213
|
+
if (key === '**') {
|
|
214
|
+
return Object.keys(data).flatMap((key)=>{
|
|
215
|
+
const nextPath = currPath.concat(key);
|
|
216
|
+
const value = data[key];
|
|
217
|
+
const set = new Set([
|
|
218
|
+
// recursively find matching sub-paths
|
|
219
|
+
...expandPropertyPath(value, segments, nextPath),
|
|
220
|
+
// skip the first remaining segment, if it matches the current key
|
|
221
|
+
...rest[0] === key ? expandPropertyPath(value, rest.slice(1), nextPath) : []
|
|
222
|
+
]);
|
|
223
|
+
return [
|
|
224
|
+
...set
|
|
225
|
+
];
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
return expandPropertyPath(data[key], rest, currPath.concat(key));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
class Container {
|
|
232
|
+
mount(...args) {
|
|
233
|
+
if (args.length < 2) {
|
|
234
|
+
throw new SyntaxError('The mount method requires at least');
|
|
235
|
+
}
|
|
236
|
+
const key = args[0];
|
|
237
|
+
let data;
|
|
238
|
+
let options = {};
|
|
239
|
+
if (args[1] instanceof Container || typeof args[1] === 'function') {
|
|
240
|
+
[, data] = args;
|
|
241
|
+
} else {
|
|
242
|
+
[, options, data] = args;
|
|
243
|
+
}
|
|
244
|
+
this.items.push({
|
|
245
|
+
...options,
|
|
246
|
+
data,
|
|
247
|
+
path: key
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
// ----------------------------------------------
|
|
251
|
+
async run(data = {}, options = {}) {
|
|
252
|
+
const output = {};
|
|
253
|
+
const errors = [];
|
|
254
|
+
const errorKeys = [];
|
|
255
|
+
for(let i = 0; i < this.items.length; i++){
|
|
256
|
+
const item = this.items[i];
|
|
257
|
+
if (!this.isContainerItemGroupIncluded(item, options.group)) {
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
const keys = expandPropertyPath(data, item.path, []);
|
|
261
|
+
for(let j = 0; j < keys.length; j++){
|
|
262
|
+
let value;
|
|
263
|
+
if (hasOwnProperty(output, keys[j])) {
|
|
264
|
+
value = output[keys[j]];
|
|
265
|
+
} else {
|
|
266
|
+
value = getPropertyPathValue(data, keys[j]);
|
|
267
|
+
}
|
|
268
|
+
const path = this.mergePaths(options.path, keys[j]);
|
|
269
|
+
const pathRaw = this.mergePaths(options.pathRaw, item.path);
|
|
270
|
+
try {
|
|
271
|
+
if (item.data instanceof Container) {
|
|
272
|
+
const tmp = await item.data.run(isObject(value) ? value : {}, {
|
|
273
|
+
group: options.group,
|
|
274
|
+
flat: true,
|
|
275
|
+
path,
|
|
276
|
+
pathRaw
|
|
277
|
+
});
|
|
278
|
+
const tmpKeys = Object.keys(tmp);
|
|
279
|
+
for(let k = 0; k < tmpKeys.length; k++){
|
|
280
|
+
output[this.mergePaths(keys[j], tmpKeys[k])] = tmp[tmpKeys[k]];
|
|
281
|
+
}
|
|
282
|
+
} else {
|
|
283
|
+
output[keys[j]] = await item.data({
|
|
284
|
+
key: keys[j],
|
|
285
|
+
path,
|
|
286
|
+
pathRaw,
|
|
287
|
+
value,
|
|
288
|
+
data
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
} catch (e) {
|
|
292
|
+
if (e instanceof ValidupValidatorError) {
|
|
293
|
+
errors.push(e);
|
|
294
|
+
} else if (e instanceof ValidupNestedError) {
|
|
295
|
+
errors.push(...e.children);
|
|
296
|
+
} else {
|
|
297
|
+
const error = new ValidupValidatorError({
|
|
298
|
+
path,
|
|
299
|
+
received: value
|
|
300
|
+
});
|
|
301
|
+
if (e instanceof Error) {
|
|
302
|
+
error.cause = e;
|
|
303
|
+
error.message = e.message;
|
|
304
|
+
}
|
|
305
|
+
errors.push(error);
|
|
306
|
+
}
|
|
307
|
+
errorKeys.push(path);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (errors.length > 0) {
|
|
312
|
+
throw new ValidupNestedError(buildErrorMessageForAttributes(errorKeys), errors);
|
|
313
|
+
}
|
|
314
|
+
if (options.defaults) {
|
|
315
|
+
const defaultKeys = Object.keys(options.defaults);
|
|
316
|
+
for(let i = 0; i < defaultKeys.length; i++){
|
|
317
|
+
if (!hasOwnProperty(output, defaultKeys[i]) || typeof output[defaultKeys[i]] === 'undefined') {
|
|
318
|
+
output[defaultKeys[i]] = options.defaults[defaultKeys[i]];
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (options.flat) {
|
|
323
|
+
return output;
|
|
324
|
+
}
|
|
325
|
+
const temp = {};
|
|
326
|
+
const keys = Object.keys(output);
|
|
327
|
+
for(let i = 0; i < keys.length; i++){
|
|
328
|
+
setPropertyPathValue(temp, keys[i], output[keys[i]]);
|
|
329
|
+
}
|
|
330
|
+
return temp;
|
|
331
|
+
}
|
|
332
|
+
isContainerItemGroupIncluded(item, group) {
|
|
333
|
+
if (group === exports.GroupKey.WILDCARD) {
|
|
334
|
+
return true;
|
|
335
|
+
}
|
|
336
|
+
if (item.group) {
|
|
337
|
+
if (Array.isArray(item.group)) {
|
|
338
|
+
if (item.group.indexOf(exports.GroupKey.WILDCARD) !== -1) {
|
|
339
|
+
return true;
|
|
340
|
+
}
|
|
341
|
+
if (group && item.group.indexOf(group) !== -1) {
|
|
342
|
+
return true;
|
|
343
|
+
}
|
|
344
|
+
} else {
|
|
345
|
+
if (item.group === exports.GroupKey.WILDCARD) {
|
|
346
|
+
return true;
|
|
347
|
+
}
|
|
348
|
+
if (item.group === group) {
|
|
349
|
+
return true;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return false;
|
|
353
|
+
}
|
|
354
|
+
return !group;
|
|
355
|
+
}
|
|
356
|
+
mergePaths(...args) {
|
|
357
|
+
let output = '';
|
|
358
|
+
for(let i = 0; i < args.length; i++){
|
|
359
|
+
const arg = args[i];
|
|
360
|
+
if (!arg) {
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
if (arg.at(0) === '.') {
|
|
364
|
+
output += arg;
|
|
365
|
+
} else {
|
|
366
|
+
output += output.length > 0 ? `.${arg}` : arg;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return output;
|
|
370
|
+
}
|
|
371
|
+
// ----------------------------------------------
|
|
372
|
+
constructor(){
|
|
373
|
+
this.items = [];
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
exports.Container = Container;
|
|
378
|
+
exports.ValidupError = ValidupError;
|
|
379
|
+
exports.ValidupNestedError = ValidupNestedError;
|
|
380
|
+
exports.ValidupValidatorError = ValidupValidatorError;
|
|
381
|
+
exports.arrayToPropertyPath = arrayToPropertyPath;
|
|
382
|
+
exports.buildErrorMessageForAttributes = buildErrorMessageForAttributes;
|
|
383
|
+
exports.getPropertyPathValue = getPropertyPathValue;
|
|
384
|
+
exports.hasOwnProperty = hasOwnProperty;
|
|
385
|
+
exports.isObject = isObject;
|
|
386
|
+
exports.propertyPathToArray = propertyPathToArray;
|
|
387
|
+
exports.setPropertyPathValue = setPropertyPathValue;
|
|
388
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/helpers/array-to-property-path.ts","../src/helpers/error.ts","../src/utils/has-own-property.ts","../src/utils/object.ts","../src/helpers/property-path-to-array.ts","../src/helpers/get-property-path-value.ts","../src/constants.ts","../src/errors/attribute.ts","../src/errors/base.ts","../src/errors/nested.ts","../src/helpers/expand-property-path.ts","../src/module.ts"],"sourcesContent":["/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\n/**\n * @see https://github.com/express-validator/express-validator/blob/bec1dcbaa29002dcd21093ec84818c4671063b5d/src/field-selection.ts#L214\n * @param parts\n */\nexport function arrayToPropertyPath(parts: readonly string[]) : string {\n return parts.reduce((prev, segment) => {\n let part = '';\n segment = segment === '\\\\*' ? '*' : segment;\n\n // TODO: Handle brackets?\n if (segment.includes('.')) {\n // Special char key access\n part = `[\"${segment}\"]`;\n } else if (/^\\d+$/.test(segment)) {\n // Index access\n part = `[${segment}]`;\n } else if (prev) {\n // Object key access\n part = `.${segment}`;\n } else {\n // Top level key\n part = segment;\n }\n\n return prev + part;\n }, '');\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { distinctArray } from 'smob';\n\nexport function buildErrorMessageForAttributes(input: (number | string)[] | Record<string, any>) {\n let names: string[];\n if (Array.isArray(input)) {\n names = distinctArray(input);\n } else {\n names = Object.keys(input);\n }\n\n if (names.length === 0) {\n return 'An unexpected error occurred.';\n }\n\n if (names.length > 1) {\n return `The attributes ${names.join(', ')} are invalid.`;\n }\n\n return `The attribute ${String(names[0])} is invalid.`;\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport function hasOwnProperty<X extends {}, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown> {\n return Object.prototype.hasOwnProperty.call(obj, prop);\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function isObject(input: unknown) : input is Record<string, any> {\n return !!input &&\n typeof input === 'object' &&\n !Array.isArray(input);\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\n/**\n * Match property names within property paths.\n *\n * @see https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/lodash.js#L146C7-L146C118\n */\nconst rePropName = /[^.[\\]]+|\\[(?:(-?\\d+(?:\\.\\d+)?)|([\"'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2)\\]|(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))/g;\n\n/**\n * Match backslashes in property paths.\n *\n * @see https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/lodash.js#L168C1-L168C54\n */\nconst reEscapeChar = /\\\\(\\\\)?/g;\n\n/**\n * Convert string to property path array.\n *\n * @see https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/lodash.js#L6735\n * @param segment\n */\nexport function propertyPathToArray(segment: string) : string[] {\n const result = [];\n if (segment.charCodeAt(0) === 46 /* . */) {\n result.push('');\n }\n segment.replace(rePropName, (match, number, quote, subString) => {\n result.push(quote ? subString.replace(reEscapeChar, '$1') : (number || match));\n\n return match;\n });\n\n return result;\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { isObject } from '../utils';\nimport { propertyPathToArray } from './property-path-to-array';\n\nexport function getPropertyPathValue(\n data: Record<string, any>,\n path: string,\n): unknown {\n const parts = propertyPathToArray(path);\n\n let temp = data;\n let index = 0;\n while (index < parts.length) {\n if (!isObject(temp) && !Array.isArray(temp)) {\n break;\n }\n\n temp = temp[parts[index]];\n index++;\n }\n\n return temp;\n}\n\nfunction isIndex(input: string | number) : input is number {\n return typeof input === 'number' || !Number.isNaN(Number.parseInt(input, 10));\n}\n\nexport function setPropertyPathValue(\n data: Record<string, any> | Record<string, any>[],\n path: string | string[],\n value: unknown,\n) {\n const parts = Array.isArray(path) ? path : propertyPathToArray(path);\n\n let temp = data;\n let index = 0;\n while (index < parts.length) {\n if (!Array.isArray(temp) && !isObject(temp)) {\n break;\n }\n\n const key = parts[index] as keyof typeof temp;\n // [foo, '0']\n if (typeof temp[key] === 'undefined') {\n if (isIndex(key + 1)) {\n (temp as Record<string, any>)[key] = [];\n } else {\n temp[key] = {};\n }\n }\n\n if (index === parts.length - 1) {\n temp[key] = value;\n break;\n }\n\n index++;\n temp = temp[key];\n }\n\n return data;\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport enum GroupKey {\n WILDCARD = '*',\n}\n","/*\n * Copyright (c) 2024-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport type ValidupValidatorErrorOptions = {\n path: string,\n message?: string,\n code?: string | null,\n received?: unknown,\n expected?: unknown\n};\n\nexport class ValidupValidatorError extends Error {\n readonly path: string;\n\n readonly code?: string | null;\n\n readonly received?: unknown;\n\n readonly expected?: unknown;\n\n constructor(options: ValidupValidatorErrorOptions) {\n super(options.message);\n\n this.path = options.path;\n this.code = options.code;\n this.received = options.received;\n this.expected = options.expected;\n }\n}\n","/*\n * Copyright (c) 2024-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport class ValidupError extends Error {\n\n}\n","/*\n * Copyright (c) 2024-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport type { ValidupValidatorError } from './attribute';\nimport { ValidupError } from './base';\n\nexport class ValidupNestedError extends ValidupError {\n children: ValidupValidatorError[];\n\n constructor(message?: string, children?: ValidupValidatorError[]) {\n super(message);\n\n this.children = children || [];\n }\n\n addChild(child: ValidupValidatorError) {\n this.children.push(child);\n }\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { isObject } from '../utils';\nimport { arrayToPropertyPath } from './array-to-property-path';\nimport { propertyPathToArray } from './property-path-to-array';\n\nexport function expandPropertyPath(\n data: Record<string, any>,\n path: string | string[],\n currPath: readonly string[],\n): string[] {\n const segments = Array.isArray(path) ? path : propertyPathToArray(path);\n if (!segments.length) {\n // no more paths to traverse\n return [arrayToPropertyPath(currPath)];\n }\n\n const key = segments[0];\n const rest = segments.slice(1);\n\n if (\n typeof data !== 'undefined' &&\n data !== null &&\n !isObject(data) &&\n !Array.isArray(data)\n ) {\n if (key === '**' && !rest.length) {\n // globstar leaves are always selected\n return [arrayToPropertyPath(currPath)];\n }\n\n // there still are paths to traverse, but value is a primitive, stop\n return [];\n }\n\n // Use a non-null value so that non-existing fields are still selected\n data = data || {};\n\n if (key === '*') {\n return Object.keys(data)\n .flatMap((key) => expandPropertyPath(data[key], rest, currPath.concat(key)));\n }\n\n if (key === '**') {\n return Object.keys(data)\n .flatMap((key) => {\n const nextPath = currPath.concat(key);\n const value = data[key];\n const set = new Set([\n // recursively find matching sub-paths\n ...expandPropertyPath(value, segments, nextPath),\n // skip the first remaining segment, if it matches the current key\n ...(rest[0] === key ? expandPropertyPath(value, rest.slice(1), nextPath) : []),\n ]);\n\n return [...set];\n });\n }\n\n return expandPropertyPath(data[key], rest, currPath.concat(key));\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { GroupKey } from './constants';\nimport { ValidupNestedError, ValidupValidatorError } from './errors';\nimport { buildErrorMessageForAttributes, getPropertyPathValue, setPropertyPathValue } from './helpers';\nimport { expandPropertyPath } from './helpers/expand-property-path';\nimport type {\n ContainerItem,\n ContainerMountOptions,\n ContainerRunOptions,\n ObjectPropertyPath,\n ObjectPropertyPathExtended,\n Validator,\n} from './types';\nimport { hasOwnProperty, isObject } from './utils';\n\nexport class Container<\n T extends Record<string, any> = Record<string, any>,\n> {\n protected items : ContainerItem[];\n\n // ----------------------------------------------\n\n constructor() {\n this.items = [];\n }\n\n // ----------------------------------------------\n\n mount(\n key: ObjectPropertyPathExtended<T>,\n data: Container | Validator\n ) : void;\n\n mount(\n key: ObjectPropertyPathExtended<T>,\n options: ContainerMountOptions,\n data: Container | Validator\n ) : void;\n\n mount(...args: any[]) : void {\n if (args.length < 2) {\n throw new SyntaxError('The mount method requires at least');\n }\n\n const key = args[0];\n let data: Container | Validator;\n let options: ContainerMountOptions = {};\n\n if (\n args[1] instanceof Container ||\n typeof args[1] === 'function'\n ) {\n [, data] = args;\n } else {\n [, options, data] = args;\n }\n\n this.items.push({\n ...options,\n data,\n path: key,\n });\n }\n\n // ----------------------------------------------\n\n async run(\n data: Record<string, any> = {},\n options: ContainerRunOptions<T> = {},\n ): Promise<T> {\n const output: Record<string, any> = {};\n\n const errors: ValidupValidatorError[] = [];\n const errorKeys : string[] = [];\n\n for (let i = 0; i < this.items.length; i++) {\n const item = this.items[i];\n\n if (!this.isContainerItemGroupIncluded(item, options.group)) {\n continue;\n }\n\n const keys = expandPropertyPath(data, item.path, []);\n for (let j = 0; j < keys.length; j++) {\n let value : unknown;\n if (hasOwnProperty(output, keys[j])) {\n value = output[keys[j]];\n } else {\n value = getPropertyPathValue(data, keys[j]);\n }\n\n const path = this.mergePaths(options.path, keys[j]);\n const pathRaw = this.mergePaths(options.pathRaw, item.path);\n\n try {\n if (item.data instanceof Container) {\n const tmp = await item.data.run(\n isObject(value) ? value : {},\n {\n group: options.group,\n flat: true,\n path,\n pathRaw,\n // todo: extract defaults for container\n },\n );\n\n const tmpKeys = Object.keys(tmp);\n for (let k = 0; k < tmpKeys.length; k++) {\n output[this.mergePaths(keys[j], tmpKeys[k])] = tmp[tmpKeys[k]];\n }\n } else {\n output[keys[j]] = await item.data({\n key: keys[j],\n path,\n pathRaw,\n value,\n data,\n });\n }\n } catch (e) {\n if (e instanceof ValidupValidatorError) {\n errors.push(e);\n } else if (e instanceof ValidupNestedError) {\n errors.push(...e.children);\n } else {\n const error = new ValidupValidatorError({\n path,\n received: value,\n });\n\n if (e instanceof Error) {\n error.cause = e;\n error.message = e.message;\n }\n\n errors.push(error);\n }\n\n errorKeys.push(path);\n }\n }\n }\n\n if (errors.length > 0) {\n throw new ValidupNestedError(buildErrorMessageForAttributes(errorKeys), errors);\n }\n\n if (options.defaults) {\n const defaultKeys = Object.keys(options.defaults);\n for (let i = 0; i < defaultKeys.length; i++) {\n if (\n !hasOwnProperty(output, defaultKeys[i]) ||\n typeof output[defaultKeys[i]] === 'undefined'\n ) {\n output[defaultKeys[i]] = options.defaults[defaultKeys[i] as unknown as ObjectPropertyPath<T>];\n }\n }\n }\n\n if (options.flat) {\n return output as T;\n }\n\n const temp : Record<string, any> = {};\n\n const keys = Object.keys(output);\n for (let i = 0; i < keys.length; i++) {\n setPropertyPathValue(temp, keys[i], output[keys[i]]);\n }\n\n return temp as T;\n }\n\n private isContainerItemGroupIncluded(\n item: ContainerItem,\n group?: string,\n ) : boolean {\n if (group === GroupKey.WILDCARD) {\n return true;\n }\n\n if (item.group) {\n if (Array.isArray(item.group)) {\n if (item.group.indexOf(GroupKey.WILDCARD) !== -1) {\n return true;\n }\n\n if (group && item.group.indexOf(group) !== -1) {\n return true;\n }\n } else {\n if (item.group === GroupKey.WILDCARD) {\n return true;\n }\n\n if (item.group === group) {\n return true;\n }\n }\n\n return false;\n }\n\n return !group;\n }\n\n private mergePaths(...args: (string | undefined)[]) {\n let output : string = '';\n\n for (let i = 0; i < args.length; i++) {\n const arg = args[i];\n if (!arg) {\n continue;\n }\n\n if (arg.at(0) === '.') {\n output += arg;\n } else {\n output += output.length > 0 ? `.${arg}` : arg;\n }\n }\n\n return output;\n }\n}\n"],"names":["arrayToPropertyPath","parts","reduce","prev","segment","part","includes","test","buildErrorMessageForAttributes","input","names","Array","isArray","distinctArray","Object","keys","length","join","String","hasOwnProperty","obj","prop","prototype","call","isObject","rePropName","reEscapeChar","propertyPathToArray","result","charCodeAt","push","replace","match","number","quote","subString","getPropertyPathValue","data","path","temp","index","isIndex","Number","isNaN","parseInt","setPropertyPathValue","value","key","GroupKey","ValidupValidatorError","Error","constructor","options","message","code","received","expected","ValidupError","ValidupNestedError","addChild","child","children","expandPropertyPath","currPath","segments","rest","slice","flatMap","concat","nextPath","set","Set","Container","mount","args","SyntaxError","items","run","output","errors","errorKeys","i","item","isContainerItemGroupIncluded","group","j","mergePaths","pathRaw","tmp","flat","tmpKeys","k","e","error","cause","defaults","defaultKeys","WILDCARD","indexOf","arg","at"],"mappings":";;;;AAAA;;;;;;;;IAWO,SAASA,mBAAAA,CAAoBC,KAAwB,EAAA;AACxD,IAAA,OAAOA,KAAMC,CAAAA,MAAM,CAAC,CAACC,IAAMC,EAAAA,OAAAA,GAAAA;AACvB,QAAA,IAAIC,IAAO,GAAA,EAAA,CAAA;QACXD,OAAUA,GAAAA,OAAAA,KAAY,QAAQ,GAAMA,GAAAA,OAAAA,CAAAA;;QAGpC,IAAIA,OAAAA,CAAQE,QAAQ,CAAC,GAAM,CAAA,EAAA;;AA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;;;;;;;;;;;"}
|
package/dist/index.d.ts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
import { distinctArray } from 'smob';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright (c) 2024.
|
|
5
|
+
* Author Peter Placzek (tada5hi)
|
|
6
|
+
* For the full copyright and license information,
|
|
7
|
+
* view the LICENSE file that was distributed with this source code.
|
|
8
|
+
*/ /**
|
|
9
|
+
* @see https://github.com/express-validator/express-validator/blob/bec1dcbaa29002dcd21093ec84818c4671063b5d/src/field-selection.ts#L214
|
|
10
|
+
* @param parts
|
|
11
|
+
*/ function arrayToPropertyPath(parts) {
|
|
12
|
+
return parts.reduce((prev, segment)=>{
|
|
13
|
+
let part = '';
|
|
14
|
+
segment = segment === '\\*' ? '*' : segment;
|
|
15
|
+
// TODO: Handle brackets?
|
|
16
|
+
if (segment.includes('.')) {
|
|
17
|
+
// Special char key access
|
|
18
|
+
part = `["${segment}"]`;
|
|
19
|
+
} else if (/^\d+$/.test(segment)) {
|
|
20
|
+
// Index access
|
|
21
|
+
part = `[${segment}]`;
|
|
22
|
+
} else if (prev) {
|
|
23
|
+
// Object key access
|
|
24
|
+
part = `.${segment}`;
|
|
25
|
+
} else {
|
|
26
|
+
// Top level key
|
|
27
|
+
part = segment;
|
|
28
|
+
}
|
|
29
|
+
return prev + part;
|
|
30
|
+
}, '');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function buildErrorMessageForAttributes(input) {
|
|
34
|
+
let names;
|
|
35
|
+
if (Array.isArray(input)) {
|
|
36
|
+
names = distinctArray(input);
|
|
37
|
+
} else {
|
|
38
|
+
names = Object.keys(input);
|
|
39
|
+
}
|
|
40
|
+
if (names.length === 0) {
|
|
41
|
+
return 'An unexpected error occurred.';
|
|
42
|
+
}
|
|
43
|
+
if (names.length > 1) {
|
|
44
|
+
return `The attributes ${names.join(', ')} are invalid.`;
|
|
45
|
+
}
|
|
46
|
+
return `The attribute ${String(names[0])} is invalid.`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/*
|
|
50
|
+
* Copyright (c) 2024.
|
|
51
|
+
* Author Peter Placzek (tada5hi)
|
|
52
|
+
* For the full copyright and license information,
|
|
53
|
+
* view the LICENSE file that was distributed with this source code.
|
|
54
|
+
*/ // eslint-disable-next-line @typescript-eslint/ban-types
|
|
55
|
+
function hasOwnProperty(obj, prop) {
|
|
56
|
+
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/*
|
|
60
|
+
* Copyright (c) 2024.
|
|
61
|
+
* Author Peter Placzek (tada5hi)
|
|
62
|
+
* For the full copyright and license information,
|
|
63
|
+
* view the LICENSE file that was distributed with this source code.
|
|
64
|
+
*/ function isObject(input) {
|
|
65
|
+
return !!input && typeof input === 'object' && !Array.isArray(input);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/*
|
|
69
|
+
* Copyright (c) 2024.
|
|
70
|
+
* Author Peter Placzek (tada5hi)
|
|
71
|
+
* For the full copyright and license information,
|
|
72
|
+
* view the LICENSE file that was distributed with this source code.
|
|
73
|
+
*/ /**
|
|
74
|
+
* Match property names within property paths.
|
|
75
|
+
*
|
|
76
|
+
* @see https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/lodash.js#L146C7-L146C118
|
|
77
|
+
*/ const rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g;
|
|
78
|
+
/**
|
|
79
|
+
* Match backslashes in property paths.
|
|
80
|
+
*
|
|
81
|
+
* @see https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/lodash.js#L168C1-L168C54
|
|
82
|
+
*/ const reEscapeChar = /\\(\\)?/g;
|
|
83
|
+
/**
|
|
84
|
+
* Convert string to property path array.
|
|
85
|
+
*
|
|
86
|
+
* @see https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/lodash.js#L6735
|
|
87
|
+
* @param segment
|
|
88
|
+
*/ function propertyPathToArray(segment) {
|
|
89
|
+
const result = [];
|
|
90
|
+
if (segment.charCodeAt(0) === 46 /* . */ ) {
|
|
91
|
+
result.push('');
|
|
92
|
+
}
|
|
93
|
+
segment.replace(rePropName, (match, number, quote, subString)=>{
|
|
94
|
+
result.push(quote ? subString.replace(reEscapeChar, '$1') : number || match);
|
|
95
|
+
return match;
|
|
96
|
+
});
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function getPropertyPathValue(data, path) {
|
|
101
|
+
const parts = propertyPathToArray(path);
|
|
102
|
+
let temp = data;
|
|
103
|
+
let index = 0;
|
|
104
|
+
while(index < parts.length){
|
|
105
|
+
if (!isObject(temp) && !Array.isArray(temp)) {
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
temp = temp[parts[index]];
|
|
109
|
+
index++;
|
|
110
|
+
}
|
|
111
|
+
return temp;
|
|
112
|
+
}
|
|
113
|
+
function isIndex(input) {
|
|
114
|
+
return typeof input === 'number' || !Number.isNaN(Number.parseInt(input, 10));
|
|
115
|
+
}
|
|
116
|
+
function setPropertyPathValue(data, path, value) {
|
|
117
|
+
const parts = Array.isArray(path) ? path : propertyPathToArray(path);
|
|
118
|
+
let temp = data;
|
|
119
|
+
let index = 0;
|
|
120
|
+
while(index < parts.length){
|
|
121
|
+
if (!Array.isArray(temp) && !isObject(temp)) {
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
const key = parts[index];
|
|
125
|
+
// [foo, '0']
|
|
126
|
+
if (typeof temp[key] === 'undefined') {
|
|
127
|
+
if (isIndex(key + 1)) {
|
|
128
|
+
temp[key] = [];
|
|
129
|
+
} else {
|
|
130
|
+
temp[key] = {};
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (index === parts.length - 1) {
|
|
134
|
+
temp[key] = value;
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
index++;
|
|
138
|
+
temp = temp[key];
|
|
139
|
+
}
|
|
140
|
+
return data;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/*
|
|
144
|
+
* Copyright (c) 2024.
|
|
145
|
+
* Author Peter Placzek (tada5hi)
|
|
146
|
+
* For the full copyright and license information,
|
|
147
|
+
* view the LICENSE file that was distributed with this source code.
|
|
148
|
+
*/ var GroupKey;
|
|
149
|
+
(function(GroupKey) {
|
|
150
|
+
GroupKey["WILDCARD"] = "*";
|
|
151
|
+
})(GroupKey || (GroupKey = {}));
|
|
152
|
+
|
|
153
|
+
/*
|
|
154
|
+
* Copyright (c) 2024-2024.
|
|
155
|
+
* Author Peter Placzek (tada5hi)
|
|
156
|
+
* For the full copyright and license information,
|
|
157
|
+
* view the LICENSE file that was distributed with this source code.
|
|
158
|
+
*/ class ValidupValidatorError extends Error {
|
|
159
|
+
constructor(options){
|
|
160
|
+
super(options.message);
|
|
161
|
+
this.path = options.path;
|
|
162
|
+
this.code = options.code;
|
|
163
|
+
this.received = options.received;
|
|
164
|
+
this.expected = options.expected;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/*
|
|
169
|
+
* Copyright (c) 2024-2024.
|
|
170
|
+
* Author Peter Placzek (tada5hi)
|
|
171
|
+
* For the full copyright and license information,
|
|
172
|
+
* view the LICENSE file that was distributed with this source code.
|
|
173
|
+
*/ class ValidupError extends Error {
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
class ValidupNestedError extends ValidupError {
|
|
177
|
+
addChild(child) {
|
|
178
|
+
this.children.push(child);
|
|
179
|
+
}
|
|
180
|
+
constructor(message, children){
|
|
181
|
+
super(message);
|
|
182
|
+
this.children = children || [];
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function expandPropertyPath(data, path, currPath) {
|
|
187
|
+
const segments = Array.isArray(path) ? path : propertyPathToArray(path);
|
|
188
|
+
if (!segments.length) {
|
|
189
|
+
// no more paths to traverse
|
|
190
|
+
return [
|
|
191
|
+
arrayToPropertyPath(currPath)
|
|
192
|
+
];
|
|
193
|
+
}
|
|
194
|
+
const key = segments[0];
|
|
195
|
+
const rest = segments.slice(1);
|
|
196
|
+
if (typeof data !== 'undefined' && data !== null && !isObject(data) && !Array.isArray(data)) {
|
|
197
|
+
if (key === '**' && !rest.length) {
|
|
198
|
+
// globstar leaves are always selected
|
|
199
|
+
return [
|
|
200
|
+
arrayToPropertyPath(currPath)
|
|
201
|
+
];
|
|
202
|
+
}
|
|
203
|
+
// there still are paths to traverse, but value is a primitive, stop
|
|
204
|
+
return [];
|
|
205
|
+
}
|
|
206
|
+
// Use a non-null value so that non-existing fields are still selected
|
|
207
|
+
data = data || {};
|
|
208
|
+
if (key === '*') {
|
|
209
|
+
return Object.keys(data).flatMap((key)=>expandPropertyPath(data[key], rest, currPath.concat(key)));
|
|
210
|
+
}
|
|
211
|
+
if (key === '**') {
|
|
212
|
+
return Object.keys(data).flatMap((key)=>{
|
|
213
|
+
const nextPath = currPath.concat(key);
|
|
214
|
+
const value = data[key];
|
|
215
|
+
const set = new Set([
|
|
216
|
+
// recursively find matching sub-paths
|
|
217
|
+
...expandPropertyPath(value, segments, nextPath),
|
|
218
|
+
// skip the first remaining segment, if it matches the current key
|
|
219
|
+
...rest[0] === key ? expandPropertyPath(value, rest.slice(1), nextPath) : []
|
|
220
|
+
]);
|
|
221
|
+
return [
|
|
222
|
+
...set
|
|
223
|
+
];
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
return expandPropertyPath(data[key], rest, currPath.concat(key));
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
class Container {
|
|
230
|
+
mount(...args) {
|
|
231
|
+
if (args.length < 2) {
|
|
232
|
+
throw new SyntaxError('The mount method requires at least');
|
|
233
|
+
}
|
|
234
|
+
const key = args[0];
|
|
235
|
+
let data;
|
|
236
|
+
let options = {};
|
|
237
|
+
if (args[1] instanceof Container || typeof args[1] === 'function') {
|
|
238
|
+
[, data] = args;
|
|
239
|
+
} else {
|
|
240
|
+
[, options, data] = args;
|
|
241
|
+
}
|
|
242
|
+
this.items.push({
|
|
243
|
+
...options,
|
|
244
|
+
data,
|
|
245
|
+
path: key
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
// ----------------------------------------------
|
|
249
|
+
async run(data = {}, options = {}) {
|
|
250
|
+
const output = {};
|
|
251
|
+
const errors = [];
|
|
252
|
+
const errorKeys = [];
|
|
253
|
+
for(let i = 0; i < this.items.length; i++){
|
|
254
|
+
const item = this.items[i];
|
|
255
|
+
if (!this.isContainerItemGroupIncluded(item, options.group)) {
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
const keys = expandPropertyPath(data, item.path, []);
|
|
259
|
+
for(let j = 0; j < keys.length; j++){
|
|
260
|
+
let value;
|
|
261
|
+
if (hasOwnProperty(output, keys[j])) {
|
|
262
|
+
value = output[keys[j]];
|
|
263
|
+
} else {
|
|
264
|
+
value = getPropertyPathValue(data, keys[j]);
|
|
265
|
+
}
|
|
266
|
+
const path = this.mergePaths(options.path, keys[j]);
|
|
267
|
+
const pathRaw = this.mergePaths(options.pathRaw, item.path);
|
|
268
|
+
try {
|
|
269
|
+
if (item.data instanceof Container) {
|
|
270
|
+
const tmp = await item.data.run(isObject(value) ? value : {}, {
|
|
271
|
+
group: options.group,
|
|
272
|
+
flat: true,
|
|
273
|
+
path,
|
|
274
|
+
pathRaw
|
|
275
|
+
});
|
|
276
|
+
const tmpKeys = Object.keys(tmp);
|
|
277
|
+
for(let k = 0; k < tmpKeys.length; k++){
|
|
278
|
+
output[this.mergePaths(keys[j], tmpKeys[k])] = tmp[tmpKeys[k]];
|
|
279
|
+
}
|
|
280
|
+
} else {
|
|
281
|
+
output[keys[j]] = await item.data({
|
|
282
|
+
key: keys[j],
|
|
283
|
+
path,
|
|
284
|
+
pathRaw,
|
|
285
|
+
value,
|
|
286
|
+
data
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
} catch (e) {
|
|
290
|
+
if (e instanceof ValidupValidatorError) {
|
|
291
|
+
errors.push(e);
|
|
292
|
+
} else if (e instanceof ValidupNestedError) {
|
|
293
|
+
errors.push(...e.children);
|
|
294
|
+
} else {
|
|
295
|
+
const error = new ValidupValidatorError({
|
|
296
|
+
path,
|
|
297
|
+
received: value
|
|
298
|
+
});
|
|
299
|
+
if (e instanceof Error) {
|
|
300
|
+
error.cause = e;
|
|
301
|
+
error.message = e.message;
|
|
302
|
+
}
|
|
303
|
+
errors.push(error);
|
|
304
|
+
}
|
|
305
|
+
errorKeys.push(path);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
if (errors.length > 0) {
|
|
310
|
+
throw new ValidupNestedError(buildErrorMessageForAttributes(errorKeys), errors);
|
|
311
|
+
}
|
|
312
|
+
if (options.defaults) {
|
|
313
|
+
const defaultKeys = Object.keys(options.defaults);
|
|
314
|
+
for(let i = 0; i < defaultKeys.length; i++){
|
|
315
|
+
if (!hasOwnProperty(output, defaultKeys[i]) || typeof output[defaultKeys[i]] === 'undefined') {
|
|
316
|
+
output[defaultKeys[i]] = options.defaults[defaultKeys[i]];
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if (options.flat) {
|
|
321
|
+
return output;
|
|
322
|
+
}
|
|
323
|
+
const temp = {};
|
|
324
|
+
const keys = Object.keys(output);
|
|
325
|
+
for(let i = 0; i < keys.length; i++){
|
|
326
|
+
setPropertyPathValue(temp, keys[i], output[keys[i]]);
|
|
327
|
+
}
|
|
328
|
+
return temp;
|
|
329
|
+
}
|
|
330
|
+
isContainerItemGroupIncluded(item, group) {
|
|
331
|
+
if (group === GroupKey.WILDCARD) {
|
|
332
|
+
return true;
|
|
333
|
+
}
|
|
334
|
+
if (item.group) {
|
|
335
|
+
if (Array.isArray(item.group)) {
|
|
336
|
+
if (item.group.indexOf(GroupKey.WILDCARD) !== -1) {
|
|
337
|
+
return true;
|
|
338
|
+
}
|
|
339
|
+
if (group && item.group.indexOf(group) !== -1) {
|
|
340
|
+
return true;
|
|
341
|
+
}
|
|
342
|
+
} else {
|
|
343
|
+
if (item.group === GroupKey.WILDCARD) {
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
if (item.group === group) {
|
|
347
|
+
return true;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
return false;
|
|
351
|
+
}
|
|
352
|
+
return !group;
|
|
353
|
+
}
|
|
354
|
+
mergePaths(...args) {
|
|
355
|
+
let output = '';
|
|
356
|
+
for(let i = 0; i < args.length; i++){
|
|
357
|
+
const arg = args[i];
|
|
358
|
+
if (!arg) {
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
if (arg.at(0) === '.') {
|
|
362
|
+
output += arg;
|
|
363
|
+
} else {
|
|
364
|
+
output += output.length > 0 ? `.${arg}` : arg;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return output;
|
|
368
|
+
}
|
|
369
|
+
// ----------------------------------------------
|
|
370
|
+
constructor(){
|
|
371
|
+
this.items = [];
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
export { Container, GroupKey, ValidupError, ValidupNestedError, ValidupValidatorError, arrayToPropertyPath, buildErrorMessageForAttributes, getPropertyPathValue, hasOwnProperty, isObject, propertyPathToArray, setPropertyPathValue };
|
|
376
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/helpers/array-to-property-path.ts","../src/helpers/error.ts","../src/utils/has-own-property.ts","../src/utils/object.ts","../src/helpers/property-path-to-array.ts","../src/helpers/get-property-path-value.ts","../src/constants.ts","../src/errors/attribute.ts","../src/errors/base.ts","../src/errors/nested.ts","../src/helpers/expand-property-path.ts","../src/module.ts"],"sourcesContent":["/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\n/**\n * @see https://github.com/express-validator/express-validator/blob/bec1dcbaa29002dcd21093ec84818c4671063b5d/src/field-selection.ts#L214\n * @param parts\n */\nexport function arrayToPropertyPath(parts: readonly string[]) : string {\n return parts.reduce((prev, segment) => {\n let part = '';\n segment = segment === '\\\\*' ? '*' : segment;\n\n // TODO: Handle brackets?\n if (segment.includes('.')) {\n // Special char key access\n part = `[\"${segment}\"]`;\n } else if (/^\\d+$/.test(segment)) {\n // Index access\n part = `[${segment}]`;\n } else if (prev) {\n // Object key access\n part = `.${segment}`;\n } else {\n // Top level key\n part = segment;\n }\n\n return prev + part;\n }, '');\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { distinctArray } from 'smob';\n\nexport function buildErrorMessageForAttributes(input: (number | string)[] | Record<string, any>) {\n let names: string[];\n if (Array.isArray(input)) {\n names = distinctArray(input);\n } else {\n names = Object.keys(input);\n }\n\n if (names.length === 0) {\n return 'An unexpected error occurred.';\n }\n\n if (names.length > 1) {\n return `The attributes ${names.join(', ')} are invalid.`;\n }\n\n return `The attribute ${String(names[0])} is invalid.`;\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport function hasOwnProperty<X extends {}, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown> {\n return Object.prototype.hasOwnProperty.call(obj, prop);\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function isObject(input: unknown) : input is Record<string, any> {\n return !!input &&\n typeof input === 'object' &&\n !Array.isArray(input);\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\n/**\n * Match property names within property paths.\n *\n * @see https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/lodash.js#L146C7-L146C118\n */\nconst rePropName = /[^.[\\]]+|\\[(?:(-?\\d+(?:\\.\\d+)?)|([\"'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2)\\]|(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))/g;\n\n/**\n * Match backslashes in property paths.\n *\n * @see https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/lodash.js#L168C1-L168C54\n */\nconst reEscapeChar = /\\\\(\\\\)?/g;\n\n/**\n * Convert string to property path array.\n *\n * @see https://github.com/lodash/lodash/blob/ddfd9b11a0126db2302cb70ec9973b66baec0975/lodash.js#L6735\n * @param segment\n */\nexport function propertyPathToArray(segment: string) : string[] {\n const result = [];\n if (segment.charCodeAt(0) === 46 /* . */) {\n result.push('');\n }\n segment.replace(rePropName, (match, number, quote, subString) => {\n result.push(quote ? subString.replace(reEscapeChar, '$1') : (number || match));\n\n return match;\n });\n\n return result;\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { isObject } from '../utils';\nimport { propertyPathToArray } from './property-path-to-array';\n\nexport function getPropertyPathValue(\n data: Record<string, any>,\n path: string,\n): unknown {\n const parts = propertyPathToArray(path);\n\n let temp = data;\n let index = 0;\n while (index < parts.length) {\n if (!isObject(temp) && !Array.isArray(temp)) {\n break;\n }\n\n temp = temp[parts[index]];\n index++;\n }\n\n return temp;\n}\n\nfunction isIndex(input: string | number) : input is number {\n return typeof input === 'number' || !Number.isNaN(Number.parseInt(input, 10));\n}\n\nexport function setPropertyPathValue(\n data: Record<string, any> | Record<string, any>[],\n path: string | string[],\n value: unknown,\n) {\n const parts = Array.isArray(path) ? path : propertyPathToArray(path);\n\n let temp = data;\n let index = 0;\n while (index < parts.length) {\n if (!Array.isArray(temp) && !isObject(temp)) {\n break;\n }\n\n const key = parts[index] as keyof typeof temp;\n // [foo, '0']\n if (typeof temp[key] === 'undefined') {\n if (isIndex(key + 1)) {\n (temp as Record<string, any>)[key] = [];\n } else {\n temp[key] = {};\n }\n }\n\n if (index === parts.length - 1) {\n temp[key] = value;\n break;\n }\n\n index++;\n temp = temp[key];\n }\n\n return data;\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport enum GroupKey {\n WILDCARD = '*',\n}\n","/*\n * Copyright (c) 2024-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport type ValidupValidatorErrorOptions = {\n path: string,\n message?: string,\n code?: string | null,\n received?: unknown,\n expected?: unknown\n};\n\nexport class ValidupValidatorError extends Error {\n readonly path: string;\n\n readonly code?: string | null;\n\n readonly received?: unknown;\n\n readonly expected?: unknown;\n\n constructor(options: ValidupValidatorErrorOptions) {\n super(options.message);\n\n this.path = options.path;\n this.code = options.code;\n this.received = options.received;\n this.expected = options.expected;\n }\n}\n","/*\n * Copyright (c) 2024-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport class ValidupError extends Error {\n\n}\n","/*\n * Copyright (c) 2024-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport type { ValidupValidatorError } from './attribute';\nimport { ValidupError } from './base';\n\nexport class ValidupNestedError extends ValidupError {\n children: ValidupValidatorError[];\n\n constructor(message?: string, children?: ValidupValidatorError[]) {\n super(message);\n\n this.children = children || [];\n }\n\n addChild(child: ValidupValidatorError) {\n this.children.push(child);\n }\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { isObject } from '../utils';\nimport { arrayToPropertyPath } from './array-to-property-path';\nimport { propertyPathToArray } from './property-path-to-array';\n\nexport function expandPropertyPath(\n data: Record<string, any>,\n path: string | string[],\n currPath: readonly string[],\n): string[] {\n const segments = Array.isArray(path) ? path : propertyPathToArray(path);\n if (!segments.length) {\n // no more paths to traverse\n return [arrayToPropertyPath(currPath)];\n }\n\n const key = segments[0];\n const rest = segments.slice(1);\n\n if (\n typeof data !== 'undefined' &&\n data !== null &&\n !isObject(data) &&\n !Array.isArray(data)\n ) {\n if (key === '**' && !rest.length) {\n // globstar leaves are always selected\n return [arrayToPropertyPath(currPath)];\n }\n\n // there still are paths to traverse, but value is a primitive, stop\n return [];\n }\n\n // Use a non-null value so that non-existing fields are still selected\n data = data || {};\n\n if (key === '*') {\n return Object.keys(data)\n .flatMap((key) => expandPropertyPath(data[key], rest, currPath.concat(key)));\n }\n\n if (key === '**') {\n return Object.keys(data)\n .flatMap((key) => {\n const nextPath = currPath.concat(key);\n const value = data[key];\n const set = new Set([\n // recursively find matching sub-paths\n ...expandPropertyPath(value, segments, nextPath),\n // skip the first remaining segment, if it matches the current key\n ...(rest[0] === key ? expandPropertyPath(value, rest.slice(1), nextPath) : []),\n ]);\n\n return [...set];\n });\n }\n\n return expandPropertyPath(data[key], rest, currPath.concat(key));\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { GroupKey } from './constants';\nimport { ValidupNestedError, ValidupValidatorError } from './errors';\nimport { buildErrorMessageForAttributes, getPropertyPathValue, setPropertyPathValue } from './helpers';\nimport { expandPropertyPath } from './helpers/expand-property-path';\nimport type {\n ContainerItem,\n ContainerMountOptions,\n ContainerRunOptions,\n ObjectPropertyPath,\n ObjectPropertyPathExtended,\n Validator,\n} from './types';\nimport { hasOwnProperty, isObject } from './utils';\n\nexport class Container<\n T extends Record<string, any> = Record<string, any>,\n> {\n protected items : ContainerItem[];\n\n // ----------------------------------------------\n\n constructor() {\n this.items = [];\n }\n\n // ----------------------------------------------\n\n mount(\n key: ObjectPropertyPathExtended<T>,\n data: Container | Validator\n ) : void;\n\n mount(\n key: ObjectPropertyPathExtended<T>,\n options: ContainerMountOptions,\n data: Container | Validator\n ) : void;\n\n mount(...args: any[]) : void {\n if (args.length < 2) {\n throw new SyntaxError('The mount method requires at least');\n }\n\n const key = args[0];\n let data: Container | Validator;\n let options: ContainerMountOptions = {};\n\n if (\n args[1] instanceof Container ||\n typeof args[1] === 'function'\n ) {\n [, data] = args;\n } else {\n [, options, data] = args;\n }\n\n this.items.push({\n ...options,\n data,\n path: key,\n });\n }\n\n // ----------------------------------------------\n\n async run(\n data: Record<string, any> = {},\n options: ContainerRunOptions<T> = {},\n ): Promise<T> {\n const output: Record<string, any> = {};\n\n const errors: ValidupValidatorError[] = [];\n const errorKeys : string[] = [];\n\n for (let i = 0; i < this.items.length; i++) {\n const item = this.items[i];\n\n if (!this.isContainerItemGroupIncluded(item, options.group)) {\n continue;\n }\n\n const keys = expandPropertyPath(data, item.path, []);\n for (let j = 0; j < keys.length; j++) {\n let value : unknown;\n if (hasOwnProperty(output, keys[j])) {\n value = output[keys[j]];\n } else {\n value = getPropertyPathValue(data, keys[j]);\n }\n\n const path = this.mergePaths(options.path, keys[j]);\n const pathRaw = this.mergePaths(options.pathRaw, item.path);\n\n try {\n if (item.data instanceof Container) {\n const tmp = await item.data.run(\n isObject(value) ? value : {},\n {\n group: options.group,\n flat: true,\n path,\n pathRaw,\n // todo: extract defaults for container\n },\n );\n\n const tmpKeys = Object.keys(tmp);\n for (let k = 0; k < tmpKeys.length; k++) {\n output[this.mergePaths(keys[j], tmpKeys[k])] = tmp[tmpKeys[k]];\n }\n } else {\n output[keys[j]] = await item.data({\n key: keys[j],\n path,\n pathRaw,\n value,\n data,\n });\n }\n } catch (e) {\n if (e instanceof ValidupValidatorError) {\n errors.push(e);\n } else if (e instanceof ValidupNestedError) {\n errors.push(...e.children);\n } else {\n const error = new ValidupValidatorError({\n path,\n received: value,\n });\n\n if (e instanceof Error) {\n error.cause = e;\n error.message = e.message;\n }\n\n errors.push(error);\n }\n\n errorKeys.push(path);\n }\n }\n }\n\n if (errors.length > 0) {\n throw new ValidupNestedError(buildErrorMessageForAttributes(errorKeys), errors);\n }\n\n if (options.defaults) {\n const defaultKeys = Object.keys(options.defaults);\n for (let i = 0; i < defaultKeys.length; i++) {\n if (\n !hasOwnProperty(output, defaultKeys[i]) ||\n typeof output[defaultKeys[i]] === 'undefined'\n ) {\n output[defaultKeys[i]] = options.defaults[defaultKeys[i] as unknown as ObjectPropertyPath<T>];\n }\n }\n }\n\n if (options.flat) {\n return output as T;\n }\n\n const temp : Record<string, any> = {};\n\n const keys = Object.keys(output);\n for (let i = 0; i < keys.length; i++) {\n setPropertyPathValue(temp, keys[i], output[keys[i]]);\n }\n\n return temp as T;\n }\n\n private isContainerItemGroupIncluded(\n item: ContainerItem,\n group?: string,\n ) : boolean {\n if (group === GroupKey.WILDCARD) {\n return true;\n }\n\n if (item.group) {\n if (Array.isArray(item.group)) {\n if (item.group.indexOf(GroupKey.WILDCARD) !== -1) {\n return true;\n }\n\n if (group && item.group.indexOf(group) !== -1) {\n return true;\n }\n } else {\n if (item.group === GroupKey.WILDCARD) {\n return true;\n }\n\n if (item.group === group) {\n return true;\n }\n }\n\n return false;\n }\n\n return !group;\n }\n\n private mergePaths(...args: (string | undefined)[]) {\n let output : string = '';\n\n for (let i = 0; i < args.length; i++) {\n const arg = args[i];\n if (!arg) {\n continue;\n }\n\n if (arg.at(0) === '.') {\n output += arg;\n } else {\n output += output.length > 0 ? `.${arg}` : arg;\n }\n }\n\n return output;\n }\n}\n"],"names":["arrayToPropertyPath","parts","reduce","prev","segment","part","includes","test","buildErrorMessageForAttributes","input","names","Array","isArray","distinctArray","Object","keys","length","join","String","hasOwnProperty","obj","prop","prototype","call","isObject","rePropName","reEscapeChar","propertyPathToArray","result","charCodeAt","push","replace","match","number","quote","subString","getPropertyPathValue","data","path","temp","index","isIndex","Number","isNaN","parseInt","setPropertyPathValue","value","key","GroupKey","ValidupValidatorError","Error","constructor","options","message","code","received","expected","ValidupError","ValidupNestedError","addChild","child","children","expandPropertyPath","currPath","segments","rest","slice","flatMap","concat","nextPath","set","Set","Container","mount","args","SyntaxError","items","run","output","errors","errorKeys","i","item","isContainerItemGroupIncluded","group","j","mergePaths","pathRaw","tmp","flat","tmpKeys","k","e","error","cause","defaults","defaultKeys","WILDCARD","indexOf","arg","at"],"mappings":";;AAAA;;;;;;;;IAWO,SAASA,mBAAAA,CAAoBC,KAAwB,EAAA;AACxD,IAAA,OAAOA,KAAMC,CAAAA,MAAM,CAAC,CAACC,IAAMC,EAAAA,OAAAA,GAAAA;AACvB,QAAA,IAAIC,IAAO,GAAA,EAAA,CAAA;QACXD,OAAUA,GAAAA,OAAAA,KAAY,QAAQ,GAAMA,GAAAA,OAAAA,CAAAA;;QAGpC,IAAIA,OAAAA,CAAQE,QAAQ,CAAC,GAAM,CAAA,EAAA;;AAEvBD,YAAAA,IAAAA,GAAO,CAAC,EAAE,EAAED,OAAAA,CAAQ,EAAE,CAAC,CAAA;AAC3B,SAAA,MAAO,IAAI,OAAA,CAAQG,IAAI,CAACH,OAAU,CAAA,EAAA;;AAE9BC,YAAAA,IAAAA,GAAO,CAAC,CAAC,EAAED,OAAAA,CAAQ,CAAC,CAAC,CAAA;AACzB,SAAA,MAAO,IAAID,IAAM,EAAA;;AAEbE,YAAAA,IAAAA,GAAO,CAAC,CAAC,EAAED,OAAAA,CAAQ,CAAC,CAAA;SACjB,MAAA;;YAEHC,IAAOD,GAAAA,OAAAA,CAAAA;AACX,SAAA;AAEA,QAAA,OAAOD,IAAOE,GAAAA,IAAAA,CAAAA;KACf,EAAA,EAAA,CAAA,CAAA;AACP;;ACxBO,SAASG,+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A;AACZ,KAAA;IAEQE,UAAW,CAAA,GAAGZ,IAA4B,EAAE;AAChD,QAAA,IAAII,MAAkB,GAAA,EAAA,CAAA;AAEtB,QAAA,IAAK,IAAIG,CAAI,GAAA,CAAA,EAAGA,IAAIP,IAAK1D,CAAAA,MAAM,EAAEiE,CAAK,EAAA,CAAA;YAClC,MAAMkB,GAAAA,GAAMzB,IAAI,CAACO,CAAE,CAAA,CAAA;AACnB,YAAA,IAAI,CAACkB,GAAK,EAAA;AACN,gBAAA,SAAA;AACJ,aAAA;AAEA,YAAA,IAAIA,GAAIC,CAAAA,EAAE,CAAC,CAAA,CAAA,KAAO,GAAK,EAAA;gBACnBtB,MAAUqB,IAAAA,GAAAA,CAAAA;aACP,MAAA;gBACHrB,MAAUA,IAAAA,MAAAA,CAAO9D,MAAM,GAAG,CAAA,GAAI,CAAC,CAAC,EAAEmF,GAAI,CAAA,CAAC,GAAGA,GAAAA,CAAAA;AAC9C,aAAA;AACJ,SAAA;QAEA,OAAOrB,MAAAA,CAAAA;AACX,KAAA;;IA1MA3B,WAAc,EAAA;QACV,IAAI,CAACyB,KAAK,GAAG,EAAE,CAAA;AACnB,KAAA;AAyMJ;;;;"}
|
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ContainerItem, ContainerMountOptions, ContainerRunOptions, ObjectPropertyPathExtended, Validator } from './types';
|
|
2
|
+
export declare class Container<T extends Record<string, any> = Record<string, any>> {
|
|
3
|
+
protected items: ContainerItem[];
|
|
4
|
+
constructor();
|
|
5
|
+
mount(key: ObjectPropertyPathExtended<T>, data: Container | Validator): void;
|
|
6
|
+
mount(key: ObjectPropertyPathExtended<T>, options: ContainerMountOptions, data: Container | Validator): void;
|
|
7
|
+
run(data?: Record<string, any>, options?: ContainerRunOptions<T>): Promise<T>;
|
|
8
|
+
private isContainerItemGroupIncluded;
|
|
9
|
+
private mergePaths;
|
|
10
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Container } from './module';
|
|
2
|
+
export type ContainerRunOptions<T extends Record<string, any> = Record<string, any>> = {
|
|
3
|
+
defaults?: {
|
|
4
|
+
[Key in ObjectPropertyPath<T>]: any;
|
|
5
|
+
};
|
|
6
|
+
group?: string;
|
|
7
|
+
flat?: boolean;
|
|
8
|
+
path?: string;
|
|
9
|
+
pathRaw?: string;
|
|
10
|
+
};
|
|
11
|
+
export type ValidatorContext = {
|
|
12
|
+
key: string;
|
|
13
|
+
path: string;
|
|
14
|
+
pathRaw: string;
|
|
15
|
+
value: unknown;
|
|
16
|
+
data: Record<string, any>;
|
|
17
|
+
};
|
|
18
|
+
export type Validator = (ctx: ValidatorContext) => Promise<unknown> | unknown;
|
|
19
|
+
export type ContainerMountOptions = {
|
|
20
|
+
group?: string | string[];
|
|
21
|
+
};
|
|
22
|
+
export type ContainerItem = ContainerMountOptions & {
|
|
23
|
+
path: string;
|
|
24
|
+
data: Validator | Container;
|
|
25
|
+
};
|
|
26
|
+
type ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
|
|
27
|
+
type ObjectLiteral = Record<string | number, any>;
|
|
28
|
+
type PrevIndex = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
|
|
29
|
+
export type ObjectPropertyPathExtended<T, Depth extends number = 4> = [Depth] extends [0] ? never : T extends ObjectLiteral ? {
|
|
30
|
+
[Key in keyof T & (string | number)]: T[Key] extends unknown[] ? (ObjectPropertyPathExtended<ArrayElement<T[Key]>, PrevIndex[Depth]> extends string ? `${Key}[${number}].${ObjectPropertyPathExtended<ArrayElement<T[Key]>, PrevIndex[Depth]>}` | `${Key}.*.${ObjectPropertyPathExtended<ArrayElement<T[Key]>, PrevIndex[Depth]>}` | `${Key}` : `${Key}[${number}]` | `${Key}`) : T[Key] extends ObjectLiteral ? (ObjectPropertyPathExtended<T[Key]> extends string ? `${Key}.${ObjectPropertyPathExtended<T[Key], PrevIndex[Depth]>}` | `*.${ObjectPropertyPathExtended<T[Key], PrevIndex[Depth]>}` | `${Key}` : `${Key}`) : `${Key}`;
|
|
31
|
+
}[keyof T & (string | number)] : never;
|
|
32
|
+
export type ObjectPropertyPath<T, Depth extends number = 4> = [Depth] extends [0] ? never : T extends ObjectLiteral ? {
|
|
33
|
+
[Key in keyof T & (string | number)]: T[Key] extends unknown[] ? (ObjectPropertyPathExtended<ArrayElement<T[Key]>, PrevIndex[Depth]> extends string ? `${Key}[${number}].${ObjectPropertyPathExtended<ArrayElement<T[Key]>, PrevIndex[Depth]>}` | `${Key}` : `${Key}[${number}]` | `${Key}`) : T[Key] extends ObjectLiteral ? (ObjectPropertyPathExtended<T[Key]> extends string ? `${Key}.${ObjectPropertyPathExtended<T[Key], PrevIndex[Depth]>}` | `${Key}` : `${Key}`) : `${Key}`;
|
|
34
|
+
}[keyof T & (string | number)] : never;
|
|
35
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function hasOwnProperty<X extends {}, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isObject(input: unknown): input is Record<string, any>;
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "validup",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A validation library.",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Peter Placzek",
|
|
7
|
+
"email": "contact@tada5hi.net",
|
|
8
|
+
"url": "https://github.com/tada5hi"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/tada5hi/validup.git"
|
|
13
|
+
},
|
|
14
|
+
"main": "dist/index.cjs",
|
|
15
|
+
"module": "dist/index.mjs",
|
|
16
|
+
"types": "dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
"./package.json": "./package.json",
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.mjs",
|
|
22
|
+
"require": "./dist/index.cjs"
|
|
23
|
+
},
|
|
24
|
+
"./bin/*": "./bin/*"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"bin",
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=18.0.0"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build:types": "tsc --emitDeclarationOnly -p tsconfig.build.json",
|
|
35
|
+
"build:js": "rollup -c",
|
|
36
|
+
"build": "rimraf dist && rimraf bin && npm run build:types && npm run build:js",
|
|
37
|
+
"test": "cross-env NODE_ENV=test jest --config ./test/jest.config.js",
|
|
38
|
+
"test:coverage": "cross-env NODE_ENV=test jest --config ./test/jest.config.js --coverage"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [],
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"smob": "^1.5.0"
|
|
44
|
+
}
|
|
45
|
+
}
|