@feathersjs/schema 5.0.0-pre.11 → 5.0.0-pre.17
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 +66 -0
- package/LICENSE +1 -1
- package/README.md +1 -2
- package/lib/hooks.d.ts +5 -5
- package/lib/hooks.js +27 -16
- package/lib/hooks.js.map +1 -1
- package/lib/query.d.ts +1 -8
- package/lib/query.js +2 -10
- package/lib/query.js.map +1 -1
- package/lib/resolver.d.ts +5 -3
- package/lib/resolver.js +8 -4
- package/lib/resolver.js.map +1 -1
- package/lib/schema.d.ts +7 -8
- package/lib/schema.js +13 -16
- package/lib/schema.js.map +1 -1
- package/package.json +15 -13
- package/src/hooks.ts +32 -21
- package/src/query.ts +1 -9
- package/src/resolver.ts +9 -6
- package/src/schema.ts +18 -26
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,72 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [5.0.0-pre.17](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.16...v5.0.0-pre.17) (2022-02-15)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* **hooks:** Allow all built-in hooks to be used the async and regular way ([#2559](https://github.com/feathersjs/feathers/issues/2559)) ([8f9f631](https://github.com/feathersjs/feathers/commit/8f9f631e0ce89de349207db72def84e7ab496a4a))
|
|
12
|
+
* **queryProperty:** allow compound oneOf ([#2545](https://github.com/feathersjs/feathers/issues/2545)) ([3077d2d](https://github.com/feathersjs/feathers/commit/3077d2d896a38d579ce4d5b530e21ad332bcf221))
|
|
13
|
+
* **schema:** Properly handle resolver errors ([#2540](https://github.com/feathersjs/feathers/issues/2540)) ([31fbdff](https://github.com/feathersjs/feathers/commit/31fbdff8bd848ac7e0eda56e307ac34b1bfcf17f))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# [5.0.0-pre.16](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.15...v5.0.0-pre.16) (2022-01-12)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### Bug Fixes
|
|
23
|
+
|
|
24
|
+
* **schema:** Do not error for schemas without properties ([#2519](https://github.com/feathersjs/feathers/issues/2519)) ([96fdb47](https://github.com/feathersjs/feathers/commit/96fdb47d45fd88a8039aa9cc9ec8aebd98672b95))
|
|
25
|
+
* **schema:** Fix resolver data type and use new validation feature in test fixture ([#2523](https://github.com/feathersjs/feathers/issues/2523)) ([1093f12](https://github.com/feathersjs/feathers/commit/1093f124b60524cbd9050fcf07ddaf1d558973da))
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### Features
|
|
29
|
+
|
|
30
|
+
* **schema:** Allow to use custom AJV and test with ajv-formats ([#2513](https://github.com/feathersjs/feathers/issues/2513)) ([ecfa4df](https://github.com/feathersjs/feathers/commit/ecfa4df29f029f6ca8517cacf518c14b46ffeb4e))
|
|
31
|
+
* **schema:** Improve schema typing, validation and extensibility ([#2521](https://github.com/feathersjs/feathers/issues/2521)) ([8c1b350](https://github.com/feathersjs/feathers/commit/8c1b35052792e82d13be03c06583534284fbae82))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# [5.0.0-pre.15](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.14...v5.0.0-pre.15) (2021-11-27)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
### Bug Fixes
|
|
41
|
+
|
|
42
|
+
* **typescript:** Overall typing improvements ([#2478](https://github.com/feathersjs/feathers/issues/2478)) ([b8eb804](https://github.com/feathersjs/feathers/commit/b8eb804158556d9651a8607e3c3fda15e0bfd110))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
# [5.0.0-pre.14](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.13...v5.0.0-pre.14) (2021-10-13)
|
|
49
|
+
|
|
50
|
+
**Note:** Version bump only for package @feathersjs/schema
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
# [5.0.0-pre.13](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.12...v5.0.0-pre.13) (2021-10-13)
|
|
57
|
+
|
|
58
|
+
**Note:** Version bump only for package @feathersjs/schema
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
# [5.0.0-pre.12](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.11...v5.0.0-pre.12) (2021-10-12)
|
|
65
|
+
|
|
66
|
+
**Note:** Version bump only for package @feathersjs/schema
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
6
72
|
# [5.0.0-pre.11](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.10...v5.0.0-pre.11) (2021-10-06)
|
|
7
73
|
|
|
8
74
|
|
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# @feathersjs/schema
|
|
2
2
|
|
|
3
3
|
[](https://github.com/feathersjs/feathers/actions?query=workflow%3ACI)
|
|
4
|
-
[](https://david-dm.org/feathersjs/feathers?path=packages/schema)
|
|
5
4
|
[](https://www.npmjs.com/package/@feathersjs/schema)
|
|
6
5
|
|
|
7
6
|
> A common data schema definition format
|
|
@@ -18,6 +17,6 @@ Refer to the [Feathers documentation](https://docs.feathersjs.com) for more deta
|
|
|
18
17
|
|
|
19
18
|
## License
|
|
20
19
|
|
|
21
|
-
Copyright (c)
|
|
20
|
+
Copyright (c) 2022 [Feathers contributors](https://github.com/feathersjs/feathers/graphs/contributors)
|
|
22
21
|
|
|
23
22
|
Licensed under the [MIT license](LICENSE).
|
package/lib/hooks.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { HookContext, NextFunction } from '@feathersjs/feathers';
|
|
2
2
|
import { Resolver } from './resolver';
|
|
3
3
|
import { Schema } from './schema';
|
|
4
|
-
export declare const resolveQuery: <T>(resolver: Resolver<T, HookContext<import("@feathersjs/feathers").Application<any, any>, any>>) => (context: HookContext, next
|
|
5
|
-
export declare const resolveData: <T>(resolver: Resolver<T, HookContext<import("@feathersjs/feathers").Application<any, any>, any>>) => (context: HookContext, next
|
|
6
|
-
export declare const resolveResult: <T>(resolver: Resolver<T, HookContext<import("@feathersjs/feathers").Application<any, any>, any>>) => (context: HookContext, next
|
|
7
|
-
export declare const validateQuery: (schema: Schema<any>) => (context: HookContext, next
|
|
8
|
-
export declare const validateData: (schema: Schema<any>) => (context: HookContext, next
|
|
4
|
+
export declare const resolveQuery: <T>(resolver: Resolver<T, HookContext<import("@feathersjs/feathers").Application<any, any>, any>>) => (context: HookContext, next?: NextFunction) => Promise<any>;
|
|
5
|
+
export declare const resolveData: <T>(resolver: Resolver<T, HookContext<import("@feathersjs/feathers").Application<any, any>, any>>) => (context: HookContext, next?: NextFunction) => Promise<any>;
|
|
6
|
+
export declare const resolveResult: <T>(resolver: Resolver<T, HookContext<import("@feathersjs/feathers").Application<any, any>, any>>) => (context: HookContext, next?: NextFunction) => Promise<void>;
|
|
7
|
+
export declare const validateQuery: (schema: Schema<any>) => (context: HookContext, next?: NextFunction) => Promise<any>;
|
|
8
|
+
export declare const validateData: (schema: Schema<any>) => (context: HookContext, next?: NextFunction) => Promise<any>;
|
package/lib/hooks.js
CHANGED
|
@@ -22,7 +22,9 @@ const resolveQuery = (resolver) => async (context, next) => {
|
|
|
22
22
|
...context.params,
|
|
23
23
|
query
|
|
24
24
|
};
|
|
25
|
-
|
|
25
|
+
if (typeof next === 'function') {
|
|
26
|
+
return next();
|
|
27
|
+
}
|
|
26
28
|
};
|
|
27
29
|
exports.resolveQuery = resolveQuery;
|
|
28
30
|
const resolveData = (resolver) => async (context, next) => {
|
|
@@ -37,24 +39,29 @@ const resolveData = (resolver) => async (context, next) => {
|
|
|
37
39
|
else {
|
|
38
40
|
context.data = await resolver.resolve(data, ctx, status);
|
|
39
41
|
}
|
|
40
|
-
|
|
42
|
+
if (typeof next === 'function') {
|
|
43
|
+
return next();
|
|
44
|
+
}
|
|
41
45
|
};
|
|
42
46
|
exports.resolveData = resolveData;
|
|
43
47
|
const resolveResult = (resolver) => async (context, next) => {
|
|
44
48
|
var _a;
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
49
|
+
if (typeof next === 'function') {
|
|
50
|
+
const { $resolve: properties, ...query } = ((_a = context.params) === null || _a === void 0 ? void 0 : _a.query) || {};
|
|
51
|
+
const resolve = {
|
|
52
|
+
originalContext: context,
|
|
53
|
+
...context.params.resolve,
|
|
54
|
+
properties
|
|
55
|
+
};
|
|
56
|
+
context.params = {
|
|
57
|
+
...context.params,
|
|
58
|
+
resolve,
|
|
59
|
+
query
|
|
60
|
+
};
|
|
61
|
+
await next();
|
|
62
|
+
}
|
|
57
63
|
const ctx = getContext(context);
|
|
64
|
+
const status = context.params.resolve;
|
|
58
65
|
const data = context.method === 'find' && context.result.data
|
|
59
66
|
? context.result.data
|
|
60
67
|
: context.result;
|
|
@@ -75,7 +82,9 @@ const validateQuery = (schema) => async (context, next) => {
|
|
|
75
82
|
...context.params,
|
|
76
83
|
query
|
|
77
84
|
};
|
|
78
|
-
|
|
85
|
+
if (typeof next === 'function') {
|
|
86
|
+
return next();
|
|
87
|
+
}
|
|
79
88
|
}
|
|
80
89
|
catch (error) {
|
|
81
90
|
throw (error.ajv ? new lib_1.BadRequest(error.message, error.errors) : error);
|
|
@@ -95,7 +104,9 @@ const validateData = (schema) => async (context, next) => {
|
|
|
95
104
|
catch (error) {
|
|
96
105
|
throw (error.ajv ? new lib_1.BadRequest(error.message, error.errors) : error);
|
|
97
106
|
}
|
|
98
|
-
|
|
107
|
+
if (typeof next === 'function') {
|
|
108
|
+
return next();
|
|
109
|
+
}
|
|
99
110
|
};
|
|
100
111
|
exports.validateData = validateData;
|
|
101
112
|
//# sourceMappingURL=hooks.js.map
|
package/lib/hooks.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hooks.js","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":";;;AACA,0CAA8C;AAI9C,MAAM,UAAU,GAAG,CAAC,OAAoB,EAAE,EAAE;IAC1C,OAAO;QACL,GAAG,OAAO;QACV,MAAM,EAAE;YACN,GAAG,OAAO,CAAC,MAAM;YACjB,KAAK,EAAE,EAAE;SACV;KACF,CAAA;AACH,CAAC,CAAA;AAEM,MAAM,YAAY,GAAG,CAAK,QAAkC,EAAE,EAAE,CACrE,KAAK,EAAE,OAAoB,EAAE,
|
|
1
|
+
{"version":3,"file":"hooks.js","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":";;;AACA,0CAA8C;AAI9C,MAAM,UAAU,GAAG,CAAC,OAAoB,EAAE,EAAE;IAC1C,OAAO;QACL,GAAG,OAAO;QACV,MAAM,EAAE;YACN,GAAG,OAAO,CAAC,MAAM;YACjB,KAAK,EAAE,EAAE;SACV;KACF,CAAA;AACH,CAAC,CAAA;AAEM,MAAM,YAAY,GAAG,CAAK,QAAkC,EAAE,EAAE,CACrE,KAAK,EAAE,OAAoB,EAAE,IAAmB,EAAE,EAAE;;IAClD,MAAM,GAAG,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC;IAChC,MAAM,IAAI,GAAG,CAAA,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,0CAAE,KAAK,KAAI,EAAE,CAAC;IAC1C,MAAM,KAAK,GAAG,MAAM,QAAQ,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,EAAE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B"}
|
package/lib/query.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { JSONSchema } from 'json-schema-to-ts';
|
|
2
2
|
export declare const queryProperty: <T extends JSONSchema>(definition: T) => {
|
|
3
|
-
readonly
|
|
3
|
+
readonly anyOf: readonly [T, {
|
|
4
4
|
readonly type: "object";
|
|
5
5
|
readonly additionalProperties: false;
|
|
6
6
|
readonly properties: {
|
|
@@ -20,10 +20,3 @@ export declare const queryProperty: <T extends JSONSchema>(definition: T) => {
|
|
|
20
20
|
};
|
|
21
21
|
}];
|
|
22
22
|
};
|
|
23
|
-
export declare const queryArray: <T extends readonly string[]>(fields: T) => {
|
|
24
|
-
readonly type: "array";
|
|
25
|
-
readonly items: {
|
|
26
|
-
readonly type: "string";
|
|
27
|
-
readonly enum: T;
|
|
28
|
-
};
|
|
29
|
-
};
|
package/lib/query.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.queryProperty = void 0;
|
|
4
4
|
const queryProperty = (definition) => ({
|
|
5
|
-
|
|
5
|
+
anyOf: [
|
|
6
6
|
definition,
|
|
7
7
|
{
|
|
8
8
|
type: 'object',
|
|
@@ -26,12 +26,4 @@ const queryProperty = (definition) => ({
|
|
|
26
26
|
]
|
|
27
27
|
});
|
|
28
28
|
exports.queryProperty = queryProperty;
|
|
29
|
-
const queryArray = (fields) => ({
|
|
30
|
-
type: 'array',
|
|
31
|
-
items: {
|
|
32
|
-
type: 'string',
|
|
33
|
-
enum: fields
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
exports.queryArray = queryArray;
|
|
37
29
|
//# sourceMappingURL=query.js.map
|
package/lib/query.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.js","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":";;;AAEO,MAAM,aAAa,GAAG,CAAwB,UAAa,EAAE,EAAE,CAAC,CAAC;IACtE,KAAK,EAAE;QACL,UAAU;QACV;YACE,IAAI,EAAE,QAAQ;YACd,oBAAoB,EAAE,KAAK;YAC3B,UAAU,EAAE;gBACV,GAAG,EAAE,UAAU;gBACf,IAAI,EAAE,UAAU;gBAChB,GAAG,EAAE,UAAU;gBACf,IAAI,EAAE,UAAU;gBAChB,GAAG,EAAE,UAAU;gBACf,GAAG,EAAE;oBACH,IAAI,EAAE,OAAO;oBACb,KAAK,EAAE,UAAU;iBAClB;gBACD,IAAI,EAAE;oBACJ,IAAI,EAAE,OAAO;oBACb,KAAK,EAAE,UAAU;iBAClB;aACF;SACF;KACF;CACQ,CAAA,CAAC;AAvBC,QAAA,aAAa,iBAuBd
|
|
1
|
+
{"version":3,"file":"query.js","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":";;;AAEO,MAAM,aAAa,GAAG,CAAwB,UAAa,EAAE,EAAE,CAAC,CAAC;IACtE,KAAK,EAAE;QACL,UAAU;QACV;YACE,IAAI,EAAE,QAAQ;YACd,oBAAoB,EAAE,KAAK;YAC3B,UAAU,EAAE;gBACV,GAAG,EAAE,UAAU;gBACf,IAAI,EAAE,UAAU;gBAChB,GAAG,EAAE,UAAU;gBACf,IAAI,EAAE,UAAU;gBAChB,GAAG,EAAE,UAAU;gBACf,GAAG,EAAE;oBACH,IAAI,EAAE,OAAO;oBACb,KAAK,EAAE,UAAU;iBAClB;gBACD,IAAI,EAAE;oBACJ,IAAI,EAAE,OAAO;oBACb,KAAK,EAAE,UAAU;iBAClB;aACF;SACF;KACF;CACQ,CAAA,CAAC;AAvBC,QAAA,aAAa,iBAuBd"}
|
package/lib/resolver.d.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
export declare type PropertyResolver<T, V, C> = (value: V | undefined, obj:
|
|
1
|
+
export declare type PropertyResolver<T, V, C> = (value: V | undefined, obj: T, context: C, status: ResolverStatus<T, C>) => Promise<V | undefined>;
|
|
2
2
|
export declare type PropertyResolverMap<T, C> = {
|
|
3
3
|
[key in keyof T]?: PropertyResolver<T, T[key], C>;
|
|
4
4
|
};
|
|
5
5
|
export interface ResolverConfig<T, C> {
|
|
6
|
+
schema?: any;
|
|
7
|
+
validate?: 'before' | 'after' | false;
|
|
6
8
|
properties: PropertyResolverMap<T, C>;
|
|
7
9
|
}
|
|
8
10
|
export interface ResolverStatus<T, C> {
|
|
9
11
|
path: string[];
|
|
10
12
|
originalContext?: C;
|
|
11
|
-
properties?:
|
|
13
|
+
properties?: string[];
|
|
12
14
|
stack: PropertyResolver<T, any, C>[];
|
|
13
15
|
}
|
|
14
16
|
export declare class Resolver<T, C> {
|
|
@@ -16,6 +18,6 @@ export declare class Resolver<T, C> {
|
|
|
16
18
|
readonly _type: T;
|
|
17
19
|
constructor(options: ResolverConfig<T, C>);
|
|
18
20
|
resolveProperty<D, K extends keyof T>(name: K, data: D, context: C, status?: Partial<ResolverStatus<T, C>>): Promise<T[K]>;
|
|
19
|
-
resolve<D>(
|
|
21
|
+
resolve<D>(_data: D, context: C, status?: Partial<ResolverStatus<T, C>>): Promise<T>;
|
|
20
22
|
}
|
|
21
23
|
export declare function resolve<T, C>(options: ResolverConfig<T, C>): Resolver<T, C>;
|
package/lib/resolver.js
CHANGED
|
@@ -21,8 +21,9 @@ class Resolver {
|
|
|
21
21
|
};
|
|
22
22
|
return resolver(value, data, context, resolverStatus);
|
|
23
23
|
}
|
|
24
|
-
async resolve(
|
|
25
|
-
const { properties: resolvers } = this.options;
|
|
24
|
+
async resolve(_data, context, status) {
|
|
25
|
+
const { properties: resolvers, schema, validate } = this.options;
|
|
26
|
+
const data = schema && validate === 'before' ? await schema.validate(_data) : _data;
|
|
26
27
|
const propertyList = (Array.isArray(status === null || status === void 0 ? void 0 : status.properties)
|
|
27
28
|
? status === null || status === void 0 ? void 0 : status.properties
|
|
28
29
|
// By default get all data and resolver keys but remove duplicates
|
|
@@ -54,9 +55,12 @@ class Resolver {
|
|
|
54
55
|
}
|
|
55
56
|
}));
|
|
56
57
|
if (hasErrors) {
|
|
57
|
-
|
|
58
|
+
const propertyName = (status === null || status === void 0 ? void 0 : status.properties) ? ` ${status.properties.join('.')}` : '';
|
|
59
|
+
throw new errors_1.BadRequest('Error resolving data' + propertyName, errors);
|
|
58
60
|
}
|
|
59
|
-
return
|
|
61
|
+
return schema && validate === 'after'
|
|
62
|
+
? await schema.validate(result)
|
|
63
|
+
: result;
|
|
60
64
|
}
|
|
61
65
|
}
|
|
62
66
|
exports.Resolver = Resolver;
|
package/lib/resolver.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolver.js","sourceRoot":"","sources":["../src/resolver.ts"],"names":[],"mappings":";;;AAAA,+CAAgD;
|
|
1
|
+
{"version":3,"file":"resolver.js","sourceRoot":"","sources":["../src/resolver.ts"],"names":[],"mappings":";;;AAAA,+CAAgD;AA4BhD,MAAa,QAAQ;IAGnB,YAAoB,OAA6B;QAA7B,YAAO,GAAP,OAAO,CAAsB;IACjD,CAAC;IAED,KAAK,CAAC,eAAe,CACnB,IAAO,EACP,IAAO,EACP,OAAU,EACV,SAAwC,EAAE;QAE1C,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QAC/C,MAAM,KAAK,GAAI,IAAY,CAAC,IAAI,CAAC,CAAC;QAClC,MAAM,EAAE,IAAI,GAAG,EAAE,EAAE,KAAK,GAAG,EAAE,EAAE,GAAG,MAAM,IAAI,EAAE,CAAC;QAE/C,sCAAsC;QACtC,IAAI,KAAK,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE;YAC5B,OAAO,SAAS,CAAC;SAClB;QAED,MAAM,cAAc,GAAG;YACrB,GAAG,MAAM;YACT,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,IAAc,CAAC;YAC/B,KAAK,EAAE,CAAC,GAAG,KAAK,EAAE,QAAQ,CAAC;SAC5B,CAAA;Q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Function } from 'ajv';
|
|
3
2
|
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
|
|
4
3
|
export declare const AJV: Ajv;
|
|
5
4
|
export declare type JSONSchemaDefinition = JSONSchema & {
|
|
6
5
|
$id: string;
|
|
6
|
+
$async?: boolean;
|
|
7
7
|
};
|
|
8
8
|
export declare class Schema<S extends JSONSchemaDefinition> {
|
|
9
|
+
definition: S;
|
|
9
10
|
ajv: Ajv;
|
|
10
|
-
|
|
11
|
-
definition: JSONSchema6;
|
|
12
|
-
propertyNames: string[];
|
|
11
|
+
validator: AsyncValidateFunction;
|
|
13
12
|
readonly _type: FromSchema<S>;
|
|
14
13
|
constructor(definition: S, ajv?: Ajv);
|
|
15
|
-
|
|
16
|
-
toJSON():
|
|
14
|
+
validate<T = FromSchema<S>>(...args: Parameters<ValidateFunction<T>>): Promise<T>;
|
|
15
|
+
toJSON(): S;
|
|
17
16
|
}
|
|
18
|
-
export declare function schema<S extends JSONSchemaDefinition>(definition: S): Schema<S>;
|
|
17
|
+
export declare function schema<S extends JSONSchemaDefinition>(definition: S, ajv?: Ajv): Schema<S>;
|
package/lib/schema.js
CHANGED
|
@@ -5,38 +5,35 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.schema = exports.Schema = exports.AJV = void 0;
|
|
7
7
|
const ajv_1 = __importDefault(require("ajv"));
|
|
8
|
+
const errors_1 = require("@feathersjs/errors");
|
|
8
9
|
exports.AJV = new ajv_1.default({
|
|
9
10
|
coerceTypes: true
|
|
10
11
|
});
|
|
11
12
|
class Schema {
|
|
12
13
|
constructor(definition, ajv = exports.AJV) {
|
|
13
|
-
this.ajv = ajv;
|
|
14
14
|
this.definition = definition;
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
15
|
+
this.ajv = ajv;
|
|
16
|
+
this.validator = this.ajv.compile({
|
|
17
17
|
$async: true,
|
|
18
18
|
...this.definition
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
return new Schema(extended, this.ajv);
|
|
21
|
+
async validate(...args) {
|
|
22
|
+
try {
|
|
23
|
+
const validated = await this.validator(...args);
|
|
24
|
+
return validated;
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
throw new errors_1.BadRequest(error.message, error.errors);
|
|
28
|
+
}
|
|
32
29
|
}
|
|
33
30
|
toJSON() {
|
|
34
31
|
return this.definition;
|
|
35
32
|
}
|
|
36
33
|
}
|
|
37
34
|
exports.Schema = Schema;
|
|
38
|
-
function schema(definition) {
|
|
39
|
-
return new Schema(definition);
|
|
35
|
+
function schema(definition, ajv = exports.AJV) {
|
|
36
|
+
return new Schema(definition, ajv);
|
|
40
37
|
}
|
|
41
38
|
exports.schema = schema;
|
|
42
39
|
//# sourceMappingURL=schema.js.map
|
package/lib/schema.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":";;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":";;;;;;AAAA,8CAAmE;AAEnE,+CAAgD;AAEnC,QAAA,GAAG,GAAG,IAAI,aAAG,CAAC;IACzB,WAAW,EAAE,IAAI;CAClB,CAAC,CAAC;AAIH,MAAa,MAAM;IAKjB,YAAoB,UAAa,EAAE,MAAW,WAAG;QAA7B,eAAU,GAAV,UAAU,CAAG;QAC/B,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC;YAChC,MAAM,EAAE,IAAI;YACZ,GAAI,IAAI,CAAC,UAAkB;SAC5B,CAA0B,CAAC;IAC9B,CAAC;IAED,KAAK,CAAC,QAAQ,CAAsB,GAAG,IAAqC;QAC1E,IAAI;YACF,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,GAAG,IAAI,CAAM,CAAC;YAErD,OAAO,SAAS,CAAC;SAClB;QAAC,OAAO,KAAU,EAAE;YACnB,MAAM,IAAI,mBAAU,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;SACnD;IACH,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;CACF;AA1BD,wBA0BC;AAED,SAAgB,MAAM,CAAmC,UAAa,EAAE,MAAW,WAAG;IACpF,OAAO,IAAI,MAAM,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;AACrC,CAAC;AAFD,wBAEC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@feathersjs/schema",
|
|
3
3
|
"description": "A common data schema definition format",
|
|
4
|
-
"version": "5.0.0-pre.
|
|
4
|
+
"version": "5.0.0-pre.17",
|
|
5
5
|
"homepage": "https://feathersjs.com",
|
|
6
6
|
"main": "lib/",
|
|
7
7
|
"keywords": [
|
|
@@ -50,19 +50,21 @@
|
|
|
50
50
|
"access": "public"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@feathersjs/errors": "^5.0.0-pre.
|
|
54
|
-
"@feathersjs/feathers": "^5.0.0-pre.
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"json-schema
|
|
53
|
+
"@feathersjs/errors": "^5.0.0-pre.17",
|
|
54
|
+
"@feathersjs/feathers": "^5.0.0-pre.17",
|
|
55
|
+
"@types/json-schema": "^7.0.9",
|
|
56
|
+
"ajv": "^8.10.0",
|
|
57
|
+
"json-schema": "^0.4.0",
|
|
58
|
+
"json-schema-to-ts": "^1.6.5"
|
|
58
59
|
},
|
|
59
60
|
"devDependencies": {
|
|
60
|
-
"@feathersjs/memory": "^5.0.0-pre.
|
|
61
|
-
"@types/mocha": "^9.
|
|
62
|
-
"@types/node": "^
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
61
|
+
"@feathersjs/memory": "^5.0.0-pre.17",
|
|
62
|
+
"@types/mocha": "^9.1.0",
|
|
63
|
+
"@types/node": "^17.0.15",
|
|
64
|
+
"ajv-formats": "^2.1.1",
|
|
65
|
+
"mocha": "^9.2.0",
|
|
66
|
+
"shx": "^0.3.4",
|
|
67
|
+
"typescript": "^4.5.5"
|
|
66
68
|
},
|
|
67
|
-
"gitHead": "
|
|
69
|
+
"gitHead": "d828748e57b40abfaa15710663afed417de14a1d"
|
|
68
70
|
}
|
package/src/hooks.ts
CHANGED
|
@@ -14,7 +14,7 @@ const getContext = (context: HookContext) => {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export const resolveQuery = <T> (resolver: Resolver<T, HookContext>) =>
|
|
17
|
-
async (context: HookContext, next
|
|
17
|
+
async (context: HookContext, next?: NextFunction) => {
|
|
18
18
|
const ctx = getContext(context);
|
|
19
19
|
const data = context?.params?.query || {};
|
|
20
20
|
const query = await resolver.resolve(data, ctx, {
|
|
@@ -26,11 +26,13 @@ export const resolveQuery = <T> (resolver: Resolver<T, HookContext>) =>
|
|
|
26
26
|
query
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
if (typeof next === 'function') {
|
|
30
|
+
return next();
|
|
31
|
+
}
|
|
30
32
|
};
|
|
31
33
|
|
|
32
34
|
export const resolveData = <T> (resolver: Resolver<T, HookContext>) =>
|
|
33
|
-
async (context: HookContext, next
|
|
35
|
+
async (context: HookContext, next?: NextFunction) => {
|
|
34
36
|
const ctx = getContext(context);
|
|
35
37
|
const data = context.data;
|
|
36
38
|
const status = {
|
|
@@ -45,27 +47,32 @@ export const resolveData = <T> (resolver: Resolver<T, HookContext>) =>
|
|
|
45
47
|
context.data = await resolver.resolve(data, ctx, status);
|
|
46
48
|
}
|
|
47
49
|
|
|
48
|
-
|
|
50
|
+
if (typeof next === 'function') {
|
|
51
|
+
return next();
|
|
52
|
+
}
|
|
49
53
|
};
|
|
50
54
|
|
|
51
55
|
export const resolveResult = <T> (resolver: Resolver<T, HookContext>) =>
|
|
52
|
-
async (context: HookContext, next
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
async (context: HookContext, next?: NextFunction) => {
|
|
57
|
+
if (typeof next === 'function') {
|
|
58
|
+
const { $resolve: properties, ...query } = context.params?.query || {};
|
|
59
|
+
const resolve = {
|
|
60
|
+
originalContext: context,
|
|
61
|
+
...context.params.resolve,
|
|
62
|
+
properties
|
|
63
|
+
};
|
|
60
64
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
+
context.params = {
|
|
66
|
+
...context.params,
|
|
67
|
+
resolve,
|
|
68
|
+
query
|
|
69
|
+
}
|
|
65
70
|
|
|
66
|
-
|
|
71
|
+
await next();
|
|
72
|
+
}
|
|
67
73
|
|
|
68
74
|
const ctx = getContext(context);
|
|
75
|
+
const status = context.params.resolve;
|
|
69
76
|
const data = context.method === 'find' && context.result.data
|
|
70
77
|
? context.result.data
|
|
71
78
|
: context.result;
|
|
@@ -80,7 +87,7 @@ export const resolveResult = <T> (resolver: Resolver<T, HookContext>) =>
|
|
|
80
87
|
};
|
|
81
88
|
|
|
82
89
|
export const validateQuery = (schema: Schema<any>) =>
|
|
83
|
-
async (context: HookContext, next
|
|
90
|
+
async (context: HookContext, next?: NextFunction) => {
|
|
84
91
|
const data = context?.params?.query || {};
|
|
85
92
|
|
|
86
93
|
try {
|
|
@@ -91,14 +98,16 @@ export const validateQuery = (schema: Schema<any>) =>
|
|
|
91
98
|
query
|
|
92
99
|
}
|
|
93
100
|
|
|
94
|
-
|
|
101
|
+
if (typeof next === 'function') {
|
|
102
|
+
return next();
|
|
103
|
+
}
|
|
95
104
|
} catch (error: any) {
|
|
96
105
|
throw (error.ajv ? new BadRequest(error.message, error.errors) : error);
|
|
97
106
|
}
|
|
98
107
|
};
|
|
99
108
|
|
|
100
109
|
export const validateData = (schema: Schema<any>) =>
|
|
101
|
-
async (context: HookContext, next
|
|
110
|
+
async (context: HookContext, next?: NextFunction) => {
|
|
102
111
|
const data = context.data;
|
|
103
112
|
|
|
104
113
|
try {
|
|
@@ -113,5 +122,7 @@ export const validateData = (schema: Schema<any>) =>
|
|
|
113
122
|
throw (error.ajv ? new BadRequest(error.message, error.errors) : error);
|
|
114
123
|
}
|
|
115
124
|
|
|
116
|
-
|
|
125
|
+
if (typeof next === 'function') {
|
|
126
|
+
return next();
|
|
127
|
+
}
|
|
117
128
|
};
|
package/src/query.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { JSONSchema } from 'json-schema-to-ts';
|
|
2
2
|
|
|
3
3
|
export const queryProperty = <T extends JSONSchema> (definition: T) => ({
|
|
4
|
-
|
|
4
|
+
anyOf: [
|
|
5
5
|
definition,
|
|
6
6
|
{
|
|
7
7
|
type: 'object',
|
|
@@ -24,11 +24,3 @@ export const queryProperty = <T extends JSONSchema> (definition: T) => ({
|
|
|
24
24
|
}
|
|
25
25
|
]
|
|
26
26
|
} as const);
|
|
27
|
-
|
|
28
|
-
export const queryArray = <T extends readonly string[]> (fields: T) => ({
|
|
29
|
-
type: 'array',
|
|
30
|
-
items: {
|
|
31
|
-
type: 'string',
|
|
32
|
-
enum: fields
|
|
33
|
-
}
|
|
34
|
-
} as const);
|
package/src/resolver.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { BadRequest } from '@feathersjs/errors';
|
|
2
|
-
import { Schema } from './schema';
|
|
3
2
|
|
|
4
3
|
export type PropertyResolver<T, V, C> = (
|
|
5
4
|
value: V|undefined,
|
|
6
|
-
obj:
|
|
5
|
+
obj: T,
|
|
7
6
|
context: C,
|
|
8
7
|
status: ResolverStatus<T, C>
|
|
9
8
|
) => Promise<V|undefined>;
|
|
@@ -13,7 +12,9 @@ export type PropertyResolverMap<T, C> = {
|
|
|
13
12
|
}
|
|
14
13
|
|
|
15
14
|
export interface ResolverConfig<T, C> {
|
|
16
|
-
|
|
15
|
+
// TODO this should be `Schema<any>` but has recently produced an error, see
|
|
16
|
+
// https://github.com/ThomasAribart/json-schema-to-ts/issues/53
|
|
17
|
+
schema?: any,
|
|
17
18
|
validate?: 'before'|'after'|false,
|
|
18
19
|
properties: PropertyResolverMap<T, C>
|
|
19
20
|
}
|
|
@@ -21,7 +22,7 @@ export interface ResolverConfig<T, C> {
|
|
|
21
22
|
export interface ResolverStatus<T, C> {
|
|
22
23
|
path: string[];
|
|
23
24
|
originalContext?: C;
|
|
24
|
-
properties?:
|
|
25
|
+
properties?: string[];
|
|
25
26
|
stack: PropertyResolver<T, any, C>[];
|
|
26
27
|
}
|
|
27
28
|
|
|
@@ -52,7 +53,7 @@ export class Resolver<T, C> {
|
|
|
52
53
|
stack: [...stack, resolver]
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
return resolver(value, data, context, resolverStatus);
|
|
56
|
+
return resolver(value, data as any, context, resolverStatus);
|
|
56
57
|
}
|
|
57
58
|
|
|
58
59
|
async resolve<D> (_data: D, context: C, status?: Partial<ResolverStatus<T, C>>): Promise<T> {
|
|
@@ -94,7 +95,9 @@ export class Resolver<T, C> {
|
|
|
94
95
|
}));
|
|
95
96
|
|
|
96
97
|
if (hasErrors) {
|
|
97
|
-
|
|
98
|
+
const propertyName = status?.properties ? ` ${status.properties.join('.')}` : '';
|
|
99
|
+
|
|
100
|
+
throw new BadRequest('Error resolving data' + propertyName, errors);
|
|
98
101
|
}
|
|
99
102
|
|
|
100
103
|
return schema && validate === 'after'
|
package/src/schema.ts
CHANGED
|
@@ -1,42 +1,34 @@
|
|
|
1
|
-
import Ajv, { AsyncValidateFunction } from 'ajv';
|
|
2
|
-
import { JSONSchema6 } from 'json-schema';
|
|
1
|
+
import Ajv, { AsyncValidateFunction, ValidateFunction } from 'ajv';
|
|
3
2
|
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
|
|
3
|
+
import { BadRequest } from '@feathersjs/errors';
|
|
4
4
|
|
|
5
5
|
export const AJV = new Ajv({
|
|
6
6
|
coerceTypes: true
|
|
7
7
|
});
|
|
8
8
|
|
|
9
|
-
export type JSONSchemaDefinition = JSONSchema & { $id: string };
|
|
9
|
+
export type JSONSchemaDefinition = JSONSchema & { $id: string, $async?: boolean };
|
|
10
10
|
|
|
11
11
|
export class Schema<S extends JSONSchemaDefinition> {
|
|
12
12
|
ajv: Ajv;
|
|
13
|
-
|
|
14
|
-
definition: JSONSchema6;
|
|
15
|
-
propertyNames: string[];
|
|
13
|
+
validator: AsyncValidateFunction;
|
|
16
14
|
readonly _type!: FromSchema<S>;
|
|
17
15
|
|
|
18
|
-
constructor (definition: S, ajv: Ajv = AJV) {
|
|
16
|
+
constructor (public definition: S, ajv: Ajv = AJV) {
|
|
19
17
|
this.ajv = ajv;
|
|
20
|
-
this.
|
|
21
|
-
this.propertyNames = Object.keys(this.definition.properties)
|
|
22
|
-
this.validate = this.ajv.compile({
|
|
18
|
+
this.validator = this.ajv.compile({
|
|
23
19
|
$async: true,
|
|
24
|
-
...this.definition
|
|
25
|
-
});
|
|
20
|
+
...(this.definition as any)
|
|
21
|
+
}) as AsyncValidateFunction;
|
|
26
22
|
}
|
|
27
23
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
} as const;
|
|
38
|
-
|
|
39
|
-
return new Schema <D & S> (extended as any, this.ajv);
|
|
24
|
+
async validate <T = FromSchema<S>> (...args: Parameters<ValidateFunction<T>>) {
|
|
25
|
+
try {
|
|
26
|
+
const validated = await this.validator(...args) as T;
|
|
27
|
+
|
|
28
|
+
return validated;
|
|
29
|
+
} catch (error: any) {
|
|
30
|
+
throw new BadRequest(error.message, error.errors);
|
|
31
|
+
}
|
|
40
32
|
}
|
|
41
33
|
|
|
42
34
|
toJSON () {
|
|
@@ -44,6 +36,6 @@ export class Schema<S extends JSONSchemaDefinition> {
|
|
|
44
36
|
}
|
|
45
37
|
}
|
|
46
38
|
|
|
47
|
-
export function schema <S extends JSONSchemaDefinition> (definition: S) {
|
|
48
|
-
return new Schema(definition);
|
|
39
|
+
export function schema <S extends JSONSchemaDefinition> (definition: S, ajv: Ajv = AJV) {
|
|
40
|
+
return new Schema(definition, ajv);
|
|
49
41
|
}
|