@feathersjs/schema 5.0.0-pre.33 → 5.0.0-pre.35
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 +22 -0
- package/lib/default-schemas.d.ts +2 -2
- package/lib/hooks/resolve.d.ts +2 -2
- package/lib/hooks/resolve.js +35 -28
- package/lib/hooks/resolve.js.map +1 -1
- package/lib/hooks/validate.d.ts +1 -1
- package/lib/hooks/validate.js +6 -1
- package/lib/hooks/validate.js.map +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/json-schema.d.ts +30 -12
- package/lib/json-schema.js +54 -33
- package/lib/json-schema.js.map +1 -1
- package/lib/resolver.d.ts +28 -12
- package/lib/resolver.js +19 -9
- package/lib/resolver.js.map +1 -1
- package/lib/schema.d.ts +2 -2
- package/package.json +15 -14
- package/src/hooks/resolve.ts +14 -6
- package/src/hooks/validate.ts +8 -1
- package/src/json-schema.ts +50 -13
- package/src/resolver.ts +55 -14
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,28 @@
|
|
|
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.35](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.34...v5.0.0-pre.35) (2023-01-12)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
- **generators:** Move core code generators to shared generators package ([#2982](https://github.com/feathersjs/feathers/issues/2982)) ([0328d22](https://github.com/feathersjs/feathers/commit/0328d2292153870bc43958f73d2c6f288a8cec17))
|
|
11
|
+
- **schema:** Allow to add additional operators to the query syntax ([#2941](https://github.com/feathersjs/feathers/issues/2941)) ([f324940](https://github.com/feathersjs/feathers/commit/f324940d5795b41e8c6fc113defb0beb7ab03a0a))
|
|
12
|
+
|
|
13
|
+
# [5.0.0-pre.34](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.33...v5.0.0-pre.34) (2022-12-14)
|
|
14
|
+
|
|
15
|
+
### Bug Fixes
|
|
16
|
+
|
|
17
|
+
- **core:** `context.type` for around hooks ([#2890](https://github.com/feathersjs/feathers/issues/2890)) ([d606ac6](https://github.com/feathersjs/feathers/commit/d606ac660fd5335c95206784fea36530dd2e851a))
|
|
18
|
+
- **core:** Improve service option usage and method option typings ([#2902](https://github.com/feathersjs/feathers/issues/2902)) ([164d75c](https://github.com/feathersjs/feathers/commit/164d75c0f11139a316baa91f1762de8f8eb7da2d))
|
|
19
|
+
- **schema:** Allow query schemas with no properties, error on unsupported types ([#2904](https://github.com/feathersjs/feathers/issues/2904)) ([b66c734](https://github.com/feathersjs/feathers/commit/b66c734357478f51b2d38fa7f3eee08640cea26e))
|
|
20
|
+
|
|
21
|
+
### Features
|
|
22
|
+
|
|
23
|
+
- **adapter:** Add patch data type to adapters and refactor AdapterBase usage ([#2906](https://github.com/feathersjs/feathers/issues/2906)) ([9ddc2e6](https://github.com/feathersjs/feathers/commit/9ddc2e6b028f026f939d6af68125847e5c6734b4))
|
|
24
|
+
- **cli:** Use separate patch schema and types ([#2916](https://github.com/feathersjs/feathers/issues/2916)) ([7088af6](https://github.com/feathersjs/feathers/commit/7088af64a539dc7f1a016d832b77b98aaaf92603))
|
|
25
|
+
- **schema:** Split resolver options and property resolvers ([#2889](https://github.com/feathersjs/feathers/issues/2889)) ([4822c94](https://github.com/feathersjs/feathers/commit/4822c949812e5a1dceff3c62b2f9de4781b4d601))
|
|
26
|
+
- **schema:** Virtual property resolvers ([#2900](https://github.com/feathersjs/feathers/issues/2900)) ([7d03b57](https://github.com/feathersjs/feathers/commit/7d03b57ae2f633bdd4a368e0d5955011fbd6c329))
|
|
27
|
+
|
|
6
28
|
# [5.0.0-pre.33](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.32...v5.0.0-pre.33) (2022-11-08)
|
|
7
29
|
|
|
8
30
|
**Note:** Version bump only for package @feathersjs/schema
|
package/lib/default-schemas.d.ts
CHANGED
|
@@ -115,7 +115,7 @@ export declare const authenticationSettingsSchema: {
|
|
|
115
115
|
};
|
|
116
116
|
};
|
|
117
117
|
};
|
|
118
|
-
export
|
|
118
|
+
export type AuthenticationConfiguration = FromSchema<typeof authenticationSettingsSchema>;
|
|
119
119
|
export declare const sqlSettingsSchema: {
|
|
120
120
|
readonly type: "object";
|
|
121
121
|
readonly properties: {
|
|
@@ -502,4 +502,4 @@ export declare const defaultAppConfiguration: {
|
|
|
502
502
|
};
|
|
503
503
|
};
|
|
504
504
|
};
|
|
505
|
-
export
|
|
505
|
+
export type DefaultAppConfiguration = FromSchema<typeof defaultAppConfiguration>;
|
package/lib/hooks/resolve.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { HookContext, NextFunction } from '@feathersjs/feathers';
|
|
2
2
|
import { Resolver } from '../resolver';
|
|
3
|
-
export
|
|
3
|
+
export type ResolverSetting<H extends HookContext> = Resolver<any, H> | Resolver<any, H>[];
|
|
4
4
|
export declare const resolveQuery: <T, H extends HookContext<import("@feathersjs/feathers").Application<any, any>, any>>(...resolvers: Resolver<T, H>[]) => (context: H, next?: NextFunction) => Promise<any>;
|
|
5
5
|
export declare const resolveData: <T, H extends HookContext<import("@feathersjs/feathers").Application<any, any>, any>>(...resolvers: Resolver<T, H>[]) => (context: H, next?: NextFunction) => Promise<any>;
|
|
6
6
|
export declare const resolveResult: <T, H extends HookContext<import("@feathersjs/feathers").Application<any, any>, any>>(...resolvers: Resolver<T, H>[]) => (context: H, next?: NextFunction) => Promise<void>;
|
|
@@ -8,7 +8,7 @@ export declare const DISPATCH: unique symbol;
|
|
|
8
8
|
export declare const getDispatch: (value: any, fallback?: any) => any;
|
|
9
9
|
export declare const resolveDispatch: <T, H extends HookContext<import("@feathersjs/feathers").Application<any, any>, any>>(...resolvers: Resolver<T, H>[]) => (context: H, next?: NextFunction) => Promise<void>;
|
|
10
10
|
export declare const resolveExternal: <T, H extends HookContext<import("@feathersjs/feathers").Application<any, any>, any>>(...resolvers: Resolver<T, H>[]) => (context: H, next?: NextFunction) => Promise<void>;
|
|
11
|
-
export
|
|
11
|
+
export type ResolveAllSettings<H extends HookContext> = {
|
|
12
12
|
data?: {
|
|
13
13
|
create: Resolver<any, H>;
|
|
14
14
|
patch: Resolver<any, H>;
|
package/lib/hooks/resolve.js
CHANGED
|
@@ -58,34 +58,41 @@ const resolveData = (...resolvers) => async (context, next) => {
|
|
|
58
58
|
}
|
|
59
59
|
};
|
|
60
60
|
exports.resolveData = resolveData;
|
|
61
|
-
const resolveResult = (...resolvers) =>
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
context.
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
61
|
+
const resolveResult = (...resolvers) => {
|
|
62
|
+
const virtualProperties = new Set(resolvers.reduce((acc, current) => acc.concat(current.virtualNames), []));
|
|
63
|
+
return async (context, next) => {
|
|
64
|
+
var _a;
|
|
65
|
+
if (typeof next === 'function') {
|
|
66
|
+
const { $resolve, $select: select, ...query } = ((_a = context.params) === null || _a === void 0 ? void 0 : _a.query) || {};
|
|
67
|
+
const $select = Array.isArray(select) ? select.filter((name) => !virtualProperties.has(name)) : select;
|
|
68
|
+
const resolve = {
|
|
69
|
+
originalContext: context,
|
|
70
|
+
...context.params.resolve,
|
|
71
|
+
properties: $resolve || select
|
|
72
|
+
};
|
|
73
|
+
context.params = {
|
|
74
|
+
...context.params,
|
|
75
|
+
resolve,
|
|
76
|
+
query: {
|
|
77
|
+
...query,
|
|
78
|
+
$select
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
await next();
|
|
82
|
+
}
|
|
83
|
+
const ctx = getContext(context);
|
|
84
|
+
const status = context.params.resolve;
|
|
85
|
+
const { isPaginated, data } = getData(context);
|
|
86
|
+
const result = Array.isArray(data)
|
|
87
|
+
? await Promise.all(data.map(async (current) => runResolvers(resolvers, current, ctx, status)))
|
|
88
|
+
: await runResolvers(resolvers, data, ctx, status);
|
|
89
|
+
if (isPaginated) {
|
|
90
|
+
context.result.data = result;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
context.result = result;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
89
96
|
};
|
|
90
97
|
exports.resolveResult = resolveResult;
|
|
91
98
|
exports.DISPATCH = Symbol('@feathersjs/schema/dispatch');
|
package/lib/hooks/resolve.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolve.js","sourceRoot":"","sources":["../../src/hooks/resolve.ts"],"names":[],"mappings":";;;AACA,6CAA2C;AAG3C,MAAM,UAAU,GAAG,CAAwB,OAAU,EAAE,EAAE;IACvD,OAAO,MAAM,CAAC,MAAM,CAAC;QACnB,GAAG,OAAO;QACV,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC;YACpB,GAAG,OAAO,CAAC,MAAM;YACjB,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC;SACzB,CAAC;KACH,CAAC,CAAA;AACJ,CAAC,CAAA;AAED,MAAM,OAAO,GAAG,CAAwB,OAAU,EAAE,EAAE;IACpD,MAAM,WAAW,GAAG,OAAO,CAAC,MAAM,KAAK,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,IAAI,CAAA;IACpE,MAAM,IAAI,GAAG,WAAW,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAA;IAE/D,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,CAAA;AAC9B,CAAC,CAAA;AAED,MAA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IAAA,mBAAW,EAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;gBAE5C,UAAU,CAAC,IAAI,CAAC,KAAK,EAAE,OAAU,EAAE,IAAkB,EAAE,EAAE,CACvD,OAAO,CAAC,MAAM,KAAK,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAC3D,CAAA;aACF;QACH,CAAC,CAAC,CAAA;KACH;IAED,OAAO,IAAA,eAAO,EAAC,UAAU,CAAC,CAAA;AAC5B,CAAC,CAAA;AA1BY,QAAA,UAAU,cA0BtB"}
|
package/lib/hooks/validate.d.ts
CHANGED
|
@@ -2,4 +2,4 @@ import { HookContext, NextFunction } from '@feathersjs/feathers';
|
|
|
2
2
|
import { Schema, Validator } from '../schema';
|
|
3
3
|
import { DataValidatorMap } from '../json-schema';
|
|
4
4
|
export declare const validateQuery: <H extends HookContext<import("@feathersjs/feathers").Application<any, any>, any>>(schema: Schema<any> | Validator) => (context: H, next?: NextFunction) => Promise<any>;
|
|
5
|
-
export declare const validateData: <H extends HookContext<import("@feathersjs/feathers").Application<any, any>, any>>(schema: Schema<any> | DataValidatorMap) => (context: H, next?: NextFunction) => Promise<any>;
|
|
5
|
+
export declare const validateData: <H extends HookContext<import("@feathersjs/feathers").Application<any, any>, any>>(schema: Schema<any> | DataValidatorMap | Validator) => (context: H, next?: NextFunction) => Promise<any>;
|
package/lib/hooks/validate.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.validateData = exports.validateQuery = void 0;
|
|
4
4
|
const errors_1 = require("@feathersjs/errors");
|
|
5
|
+
const adapter_commons_1 = require("@feathersjs/adapter-commons");
|
|
5
6
|
const validateQuery = (schema) => {
|
|
6
7
|
const validator = typeof schema === 'function' ? schema : schema.validate.bind(schema);
|
|
7
8
|
return async (context, next) => {
|
|
@@ -9,6 +10,7 @@ const validateQuery = (schema) => {
|
|
|
9
10
|
const data = ((_a = context === null || context === void 0 ? void 0 : context.params) === null || _a === void 0 ? void 0 : _a.query) || {};
|
|
10
11
|
try {
|
|
11
12
|
const query = await validator(data);
|
|
13
|
+
Object.defineProperty(query, adapter_commons_1.VALIDATED, { value: true });
|
|
12
14
|
context.params = {
|
|
13
15
|
...context.params,
|
|
14
16
|
query
|
|
@@ -28,7 +30,9 @@ const validateData = (schema) => {
|
|
|
28
30
|
const data = context.data;
|
|
29
31
|
const validator = typeof schema.validate === 'function'
|
|
30
32
|
? schema.validate.bind(schema)
|
|
31
|
-
: schema
|
|
33
|
+
: typeof schema === 'function'
|
|
34
|
+
? schema
|
|
35
|
+
: schema[context.method];
|
|
32
36
|
if (validator) {
|
|
33
37
|
try {
|
|
34
38
|
if (Array.isArray(data)) {
|
|
@@ -37,6 +41,7 @@ const validateData = (schema) => {
|
|
|
37
41
|
else {
|
|
38
42
|
context.data = await validator(data);
|
|
39
43
|
}
|
|
44
|
+
Object.defineProperty(context.data, adapter_commons_1.VALIDATED, { value: true });
|
|
40
45
|
}
|
|
41
46
|
catch (error) {
|
|
42
47
|
throw error.ajv ? new errors_1.BadRequest(error.message, error.errors) : error;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate.js","sourceRoot":"","sources":["../../src/hooks/validate.ts"],"names":[],"mappings":";;;AACA,+CAA+C;
|
|
1
|
+
{"version":3,"file":"validate.js","sourceRoot":"","sources":["../../src/hooks/validate.ts"],"names":[],"mappings":";;;AACA,+CAA+C;AAC/C,iEAAuD;AAIhD,MAAM,aAAa,GAAG,CAAwB,MAA+B,EAAE,EAAE;IACtF,MAAM,SAAS,GAAc,OAAO,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IAEjG,OAAO,KAAK,EAAE,OAAU,EAAE,IAAmB,E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AAG,MAAM,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;iBAC5E;qBAAM;oBACL,OAAO,CAAC,IAAI,GAAG,MAAM,SAAS,CAAC,IAAI,CAAC,CAAA;iBACrC;gBAED,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI,EAAE,2BAAS,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;aAChE;YAAC,OAAO,KAAU,EAAE;gBACnB,MAAM,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,mBAAU,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAA;aACtE;SACF;QAED,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE;YAC9B,OAAO,IAAI,EAAE,CAAA;SACd;IACH,CAAC,CAAA;AACH,CAAC,CAAA;AA5BY,QAAA,YAAY,gBA4BxB"}
|
package/lib/index.d.ts
CHANGED
|
@@ -9,10 +9,10 @@ export * from './json-schema';
|
|
|
9
9
|
export * from './default-schemas';
|
|
10
10
|
export * as hooks from './hooks';
|
|
11
11
|
export * as jsonSchema from './json-schema';
|
|
12
|
-
export
|
|
12
|
+
export type Infer<S extends {
|
|
13
13
|
_type: any;
|
|
14
14
|
}> = S['_type'];
|
|
15
|
-
export
|
|
15
|
+
export type Combine<S extends {
|
|
16
16
|
_type: any;
|
|
17
17
|
}, U> = Pick<Infer<S>, Exclude<keyof Infer<S>, keyof U>> & U;
|
|
18
18
|
declare module '@feathersjs/feathers/lib/declarations' {
|
package/lib/json-schema.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { JSONSchema } from 'json-schema-to-ts';
|
|
2
2
|
import { JSONSchemaDefinition, Ajv, Validator } from './schema';
|
|
3
|
-
export
|
|
3
|
+
export type DataSchemaMap = {
|
|
4
4
|
create: JSONSchemaDefinition;
|
|
5
5
|
update?: JSONSchemaDefinition;
|
|
6
6
|
patch?: JSONSchemaDefinition;
|
|
7
7
|
};
|
|
8
|
-
export
|
|
8
|
+
export type DataValidatorMap = {
|
|
9
9
|
create: Validator;
|
|
10
10
|
update: Validator;
|
|
11
11
|
patch: Validator;
|
|
@@ -29,7 +29,7 @@ export declare const getValidator: <T = any, R = T>(schema: JSONSchemaDefinition
|
|
|
29
29
|
* @returns A map of validator functions
|
|
30
30
|
*/
|
|
31
31
|
export declare const getDataValidator: (def: JSONSchemaDefinition | DataSchemaMap, validator: Ajv) => DataValidatorMap;
|
|
32
|
-
export
|
|
32
|
+
export type PropertyQuery<D extends JSONSchema, X> = {
|
|
33
33
|
anyOf: [
|
|
34
34
|
D,
|
|
35
35
|
{
|
|
@@ -49,7 +49,7 @@ export declare type PropertyQuery<D extends JSONSchema> = {
|
|
|
49
49
|
type: 'array';
|
|
50
50
|
items: D;
|
|
51
51
|
};
|
|
52
|
-
};
|
|
52
|
+
} & X;
|
|
53
53
|
}
|
|
54
54
|
];
|
|
55
55
|
};
|
|
@@ -57,9 +57,12 @@ export declare type PropertyQuery<D extends JSONSchema> = {
|
|
|
57
57
|
* Create a Feathers query syntax compatible JSON schema definition for a property definition.
|
|
58
58
|
*
|
|
59
59
|
* @param def The property definition (e.g. `{ type: 'string' }`)
|
|
60
|
+
* @param extensions Additional properties to add to the query property schema
|
|
60
61
|
* @returns A JSON schema definition for the Feathers query syntax for this property.
|
|
61
62
|
*/
|
|
62
|
-
export declare const queryProperty: <T extends import("json-schema-to-ts").JSONSchema7
|
|
63
|
+
export declare const queryProperty: <T extends import("json-schema-to-ts").JSONSchema7, X extends {
|
|
64
|
+
[key: string]: import("json-schema-to-ts").JSONSchema7;
|
|
65
|
+
}>(def: T, extensions?: X) => {
|
|
63
66
|
readonly anyOf: readonly [any, {
|
|
64
67
|
readonly type: "object";
|
|
65
68
|
readonly additionalProperties: false;
|
|
@@ -77,28 +80,34 @@ export declare const queryProperty: <T extends import("json-schema-to-ts").JSONS
|
|
|
77
80
|
readonly type: "array";
|
|
78
81
|
readonly items: any;
|
|
79
82
|
};
|
|
80
|
-
};
|
|
83
|
+
} & X;
|
|
81
84
|
}];
|
|
82
85
|
};
|
|
83
86
|
/**
|
|
84
87
|
* Creates Feathers a query syntax compatible JSON schema for multiple properties.
|
|
85
88
|
*
|
|
86
89
|
* @param definitions A map of property definitions
|
|
90
|
+
* @param extensions Additional properties to add to the query property schema
|
|
87
91
|
* @returns The JSON schema definition for the Feathers query syntax for multiple properties
|
|
88
92
|
*/
|
|
89
93
|
export declare const queryProperties: <T extends {
|
|
90
94
|
[key: string]: import("json-schema-to-ts").JSONSchema7;
|
|
91
|
-
}
|
|
95
|
+
}, X extends { [K in keyof T]?: {
|
|
96
|
+
[key: string]: import("json-schema-to-ts").JSONSchema7;
|
|
97
|
+
}; }>(definitions: T, extensions?: X) => { [K_1 in keyof T]: PropertyQuery<T[K_1], X[K_1]>; };
|
|
92
98
|
/**
|
|
93
99
|
* Creates a JSON schema for the complete Feathers query syntax including `$limit`, $skip`
|
|
94
100
|
* and `$sort` and `$select` for the allowed properties.
|
|
95
101
|
*
|
|
96
102
|
* @param definition The property definitions to create the query syntax schema for
|
|
103
|
+
* @param extensions Additional properties to add to the query property schema
|
|
97
104
|
* @returns A JSON schema for the complete query syntax
|
|
98
105
|
*/
|
|
99
106
|
export declare const querySyntax: <T extends {
|
|
100
|
-
[key: string]:
|
|
101
|
-
}
|
|
107
|
+
[key: string]: import("json-schema-to-ts").JSONSchema7;
|
|
108
|
+
}, X extends { [K in keyof T]?: {
|
|
109
|
+
[key: string]: import("json-schema-to-ts").JSONSchema7;
|
|
110
|
+
}; }>(definition: T, extensions?: X) => {
|
|
102
111
|
readonly $limit: {
|
|
103
112
|
readonly type: "number";
|
|
104
113
|
readonly minimum: 0;
|
|
@@ -109,16 +118,25 @@ export declare const querySyntax: <T extends {
|
|
|
109
118
|
};
|
|
110
119
|
readonly $sort: {
|
|
111
120
|
readonly type: "object";
|
|
112
|
-
readonly properties: { [
|
|
121
|
+
readonly properties: { [K_1 in keyof T]: {
|
|
113
122
|
readonly type: 'number';
|
|
114
123
|
readonly enum: [1, -1];
|
|
115
124
|
}; };
|
|
116
125
|
};
|
|
117
126
|
readonly $select: {
|
|
118
127
|
readonly type: "array";
|
|
128
|
+
readonly maxItems: number;
|
|
119
129
|
readonly items: {
|
|
130
|
+
readonly enum?: (keyof T)[];
|
|
120
131
|
readonly type: "string";
|
|
121
|
-
readonly enum: (keyof T)[];
|
|
122
132
|
};
|
|
123
133
|
};
|
|
124
|
-
|
|
134
|
+
readonly $or: {
|
|
135
|
+
readonly type: "array";
|
|
136
|
+
readonly items: {
|
|
137
|
+
readonly type: "object";
|
|
138
|
+
readonly additionalProperties: false;
|
|
139
|
+
readonly properties: { [K_2 in keyof T]: PropertyQuery<T[K_2], X[K_2]>; };
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
} & { [K_2 in keyof T]: PropertyQuery<T[K_2], X[K_2]>; };
|
package/lib/json-schema.js
CHANGED
|
@@ -44,9 +44,10 @@ exports.getDataValidator = getDataValidator;
|
|
|
44
44
|
* Create a Feathers query syntax compatible JSON schema definition for a property definition.
|
|
45
45
|
*
|
|
46
46
|
* @param def The property definition (e.g. `{ type: 'string' }`)
|
|
47
|
+
* @param extensions Additional properties to add to the query property schema
|
|
47
48
|
* @returns A JSON schema definition for the Feathers query syntax for this property.
|
|
48
49
|
*/
|
|
49
|
-
const queryProperty = (def) => {
|
|
50
|
+
const queryProperty = (def, extensions = {}) => {
|
|
50
51
|
const definition = commons_1._.omit(def, 'default');
|
|
51
52
|
return {
|
|
52
53
|
anyOf: [
|
|
@@ -67,7 +68,8 @@ const queryProperty = (def) => {
|
|
|
67
68
|
$nin: {
|
|
68
69
|
type: 'array',
|
|
69
70
|
items: definition
|
|
70
|
-
}
|
|
71
|
+
},
|
|
72
|
+
...extensions
|
|
71
73
|
}
|
|
72
74
|
}
|
|
73
75
|
]
|
|
@@ -78,12 +80,17 @@ exports.queryProperty = queryProperty;
|
|
|
78
80
|
* Creates Feathers a query syntax compatible JSON schema for multiple properties.
|
|
79
81
|
*
|
|
80
82
|
* @param definitions A map of property definitions
|
|
83
|
+
* @param extensions Additional properties to add to the query property schema
|
|
81
84
|
* @returns The JSON schema definition for the Feathers query syntax for multiple properties
|
|
82
85
|
*/
|
|
83
|
-
const queryProperties = (definitions) => Object.keys(definitions).reduce((res, key) => {
|
|
86
|
+
const queryProperties = (definitions, extensions = {}) => Object.keys(definitions).reduce((res, key) => {
|
|
84
87
|
const result = res;
|
|
85
88
|
const definition = definitions[key];
|
|
86
|
-
|
|
89
|
+
const { $ref } = definition;
|
|
90
|
+
if ($ref) {
|
|
91
|
+
throw new Error(`Can not create query syntax schema for reference property '${key}'`);
|
|
92
|
+
}
|
|
93
|
+
result[key] = (0, exports.queryProperty)(definition, extensions[key]);
|
|
87
94
|
return result;
|
|
88
95
|
}, {});
|
|
89
96
|
exports.queryProperties = queryProperties;
|
|
@@ -92,36 +99,50 @@ exports.queryProperties = queryProperties;
|
|
|
92
99
|
* and `$sort` and `$select` for the allowed properties.
|
|
93
100
|
*
|
|
94
101
|
* @param definition The property definitions to create the query syntax schema for
|
|
102
|
+
* @param extensions Additional properties to add to the query property schema
|
|
95
103
|
* @returns A JSON schema for the complete query syntax
|
|
96
104
|
*/
|
|
97
|
-
const querySyntax = (definition) =>
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
})
|
|
105
|
+
const querySyntax = (definition, extensions = {}) => {
|
|
106
|
+
const keys = Object.keys(definition);
|
|
107
|
+
const props = (0, exports.queryProperties)(definition, extensions);
|
|
108
|
+
return {
|
|
109
|
+
$limit: {
|
|
110
|
+
type: 'number',
|
|
111
|
+
minimum: 0
|
|
112
|
+
},
|
|
113
|
+
$skip: {
|
|
114
|
+
type: 'number',
|
|
115
|
+
minimum: 0
|
|
116
|
+
},
|
|
117
|
+
$sort: {
|
|
118
|
+
type: 'object',
|
|
119
|
+
properties: keys.reduce((res, key) => {
|
|
120
|
+
const result = res;
|
|
121
|
+
result[key] = {
|
|
122
|
+
type: 'number',
|
|
123
|
+
enum: [1, -1]
|
|
124
|
+
};
|
|
125
|
+
return result;
|
|
126
|
+
}, {})
|
|
127
|
+
},
|
|
128
|
+
$select: {
|
|
129
|
+
type: 'array',
|
|
130
|
+
maxItems: keys.length,
|
|
131
|
+
items: {
|
|
132
|
+
type: 'string',
|
|
133
|
+
...(keys.length > 0 ? { enum: keys } : {})
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
$or: {
|
|
137
|
+
type: 'array',
|
|
138
|
+
items: {
|
|
139
|
+
type: 'object',
|
|
140
|
+
additionalProperties: false,
|
|
141
|
+
properties: props
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
...props
|
|
145
|
+
};
|
|
146
|
+
};
|
|
126
147
|
exports.querySyntax = querySyntax;
|
|
127
148
|
//# sourceMappingURL=json-schema.js.map
|
package/lib/json-schema.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json-schema.js","sourceRoot":"","sources":["../src/json-schema.ts"],"names":[],"mappings":";;;AAAA,iDAAuC;AAgBvC;;;;;;GAMG;AACI,MAAM,YAAY,GAAG,CAAiB,MAA4B,EAAE,SAAc,EAAmB,EAAE,CAC5G,SAAS,CAAC,OAAO,CAAC;IAChB,MAAM,EAAE,IAAI;IACZ,GAAI,MAAc;CACnB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AAC;iBACd,CAAA;gBAED,OAAO,MAAM,CAAA;YACf,CAAC,EAAE,EAA6E,CAAC;SAClF;QACD,OAAO,EAAE;YACP,IAAI,EAAE,OAAO;YACb,QAAQ,EAAE,IAAI,CAAC,MAAM;YACrB,KAAK,EAAE;gBACL,IAAI,EAAE,QAAQ;gBACd,GAAG,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,IAA0B,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aACjE;SACF;QACD,GAAG,EAAE;YACH,IAAI,EAAE,OAAO;YACb,KAAK,EAAE;gBACL,IAAI,EAAE,QAAQ;gBACd,oBAAoB,EAAE,KAAK;gBAC3B,UAAU,EAAE,KAAK;aAClB;SACF;QACD,GAAG,KAAK;KACA,CAAA;AACZ,CAAC,CAAA;AAlDY,QAAA,WAAW,eAkDvB"}
|
package/lib/resolver.d.ts
CHANGED
|
@@ -1,11 +1,30 @@
|
|
|
1
1
|
import { Schema } from './schema';
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
[key in keyof T]?: PropertyResolver<T, T[key], C>;
|
|
2
|
+
export type PropertyResolver<T, V, C> = ((value: V | undefined, obj: T, context: C, status: ResolverStatus<T, C>) => Promise<V | undefined>) & {
|
|
3
|
+
[IS_VIRTUAL]?: boolean;
|
|
5
4
|
};
|
|
6
|
-
export
|
|
7
|
-
export
|
|
5
|
+
export type VirtualResolver<T, V, C> = (obj: T, context: C, status: ResolverStatus<T, C>) => Promise<V | undefined>;
|
|
6
|
+
export declare const IS_VIRTUAL: unique symbol;
|
|
7
|
+
/**
|
|
8
|
+
* Create a resolver for a virtual property. A virtual property is a property that
|
|
9
|
+
* is computed and never has an initial value.
|
|
10
|
+
*
|
|
11
|
+
* @param virtualResolver The virtual resolver function
|
|
12
|
+
* @returns The property resolver function
|
|
13
|
+
*/
|
|
14
|
+
export declare const virtual: <T, V, C>(virtualResolver: VirtualResolver<T, V, C>) => PropertyResolver<T, V, C>;
|
|
15
|
+
export type PropertyResolverMap<T, C> = {
|
|
16
|
+
[key in keyof T]?: PropertyResolver<T, T[key], C> | ReturnType<typeof virtual<T, T[key], C>>;
|
|
17
|
+
};
|
|
18
|
+
export type ResolverConverter<T, C> = (obj: any, context: C, status: ResolverStatus<T, C>) => Promise<T | undefined>;
|
|
19
|
+
export interface ResolverOptions<T, C> {
|
|
8
20
|
schema?: Schema<T>;
|
|
21
|
+
/**
|
|
22
|
+
* A converter function that is run before property resolvers
|
|
23
|
+
* to transform the initial data into a different format.
|
|
24
|
+
*/
|
|
25
|
+
converter?: ResolverConverter<T, C>;
|
|
26
|
+
}
|
|
27
|
+
export interface ResolverConfig<T, C> extends ResolverOptions<T, C> {
|
|
9
28
|
/**
|
|
10
29
|
* @deprecated Use the `validateData` and `validateQuery` hooks explicitly instead
|
|
11
30
|
*/
|
|
@@ -14,11 +33,6 @@ export interface ResolverConfig<T, C> {
|
|
|
14
33
|
* The properties to resolve
|
|
15
34
|
*/
|
|
16
35
|
properties: PropertyResolverMap<T, C>;
|
|
17
|
-
/**
|
|
18
|
-
* A converter function that is run before property resolvers
|
|
19
|
-
* to transform the initial data into a different format.
|
|
20
|
-
*/
|
|
21
|
-
converter?: ResolverConverter<T, C>;
|
|
22
36
|
}
|
|
23
37
|
export interface ResolverStatus<T, C> {
|
|
24
38
|
path: string[];
|
|
@@ -27,9 +41,10 @@ export interface ResolverStatus<T, C> {
|
|
|
27
41
|
stack: PropertyResolver<T, any, C>[];
|
|
28
42
|
}
|
|
29
43
|
export declare class Resolver<T, C> {
|
|
30
|
-
options: ResolverConfig<T, C>;
|
|
44
|
+
readonly options: ResolverConfig<T, C>;
|
|
31
45
|
readonly _type: T;
|
|
32
|
-
|
|
46
|
+
propertyNames: (keyof T)[];
|
|
47
|
+
virtualNames: (keyof T)[];
|
|
33
48
|
constructor(options: ResolverConfig<T, C>);
|
|
34
49
|
/**
|
|
35
50
|
* Resolve a single property
|
|
@@ -50,4 +65,5 @@ export declare class Resolver<T, C> {
|
|
|
50
65
|
* @param options The configuration for the returned resolver
|
|
51
66
|
* @returns A new resolver instance
|
|
52
67
|
*/
|
|
68
|
+
export declare function resolve<T, C>(properties: PropertyResolverMap<T, C>, options?: ResolverOptions<T, C>): Resolver<T, C>;
|
|
53
69
|
export declare function resolve<T, C>(options: ResolverConfig<T, C>): Resolver<T, C>;
|
package/lib/resolver.js
CHANGED
|
@@ -1,11 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.resolve = exports.Resolver = void 0;
|
|
3
|
+
exports.resolve = exports.Resolver = exports.virtual = exports.IS_VIRTUAL = void 0;
|
|
4
4
|
const errors_1 = require("@feathersjs/errors");
|
|
5
|
+
exports.IS_VIRTUAL = Symbol('@feathersjs/schema/virtual');
|
|
6
|
+
/**
|
|
7
|
+
* Create a resolver for a virtual property. A virtual property is a property that
|
|
8
|
+
* is computed and never has an initial value.
|
|
9
|
+
*
|
|
10
|
+
* @param virtualResolver The virtual resolver function
|
|
11
|
+
* @returns The property resolver function
|
|
12
|
+
*/
|
|
13
|
+
const virtual = (virtualResolver) => {
|
|
14
|
+
const propertyResolver = async (_value, obj, context, status) => virtualResolver(obj, context, status);
|
|
15
|
+
propertyResolver[exports.IS_VIRTUAL] = true;
|
|
16
|
+
return propertyResolver;
|
|
17
|
+
};
|
|
18
|
+
exports.virtual = virtual;
|
|
5
19
|
class Resolver {
|
|
6
20
|
constructor(options) {
|
|
7
21
|
this.options = options;
|
|
8
22
|
this.propertyNames = Object.keys(options.properties);
|
|
23
|
+
this.virtualNames = this.propertyNames.filter((name) => options.properties[name][exports.IS_VIRTUAL]);
|
|
9
24
|
}
|
|
10
25
|
/**
|
|
11
26
|
* Resolve a single property
|
|
@@ -81,14 +96,9 @@ class Resolver {
|
|
|
81
96
|
}
|
|
82
97
|
}
|
|
83
98
|
exports.Resolver = Resolver;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
* @param options The configuration for the returned resolver
|
|
88
|
-
* @returns A new resolver instance
|
|
89
|
-
*/
|
|
90
|
-
function resolve(options) {
|
|
91
|
-
return new Resolver(options);
|
|
99
|
+
function resolve(properties, options) {
|
|
100
|
+
const settings = (properties.properties ? properties : { properties, ...options });
|
|
101
|
+
return new Resolver(settings);
|
|
92
102
|
}
|
|
93
103
|
exports.resolve = resolve;
|
|
94
104
|
//# sourceMappingURL=resolver.js.map
|
package/lib/resolver.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolver.js","sourceRoot":"","sources":["../src/resolver.ts"],"names":[],"mappings":";;;AAAA,+CAA+C;
|
|
1
|
+
{"version":3,"file":"resolver.js","sourceRoot":"","sources":["../src/resolver.ts"],"names":[],"mappings":";;;AAAA,+CAA+C;AAgBlC,QAAA,UAAU,GAAG,MAAM,CAAC,4BAA4B,CAAC,CAAA;AAE9D;;;;;;GAMG;AACI,MAAM,OAAO,GAAG,CAAU,eAAyC,EAAE,EAAE;IAC5E,MAAM,gBAAgB,GAA8B,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,CACzF,eAAe,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,CAAC,CAAA;IAEvC,gBAAgB,CAAC,kBAAU,CAAC,GAAG,IAAI,CAAA;IAEnC,OAAO,gBAAgB,CAAA;AACzB,CAAC,CAAA;AAPY,QAAA,OAAO,WAOnB;AAuCD,MAAa,QAAQ;IAKnB,YAA4B,OAA6B;QAA7B,YAAO,GAAP,OAAO,CAAsB;QACvD,IAAI,CAAC,aAAa,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAuB,CAAA;QAC1E,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,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E,CAClE,CAAA;IAEzB,OAAO,IAAI,QAAQ,CAAO,QAAQ,CAAC,CAAA;AACrC,CAAC;AATD,0BASC"}
|
package/lib/schema.d.ts
CHANGED
|
@@ -6,8 +6,8 @@ export { Ajv };
|
|
|
6
6
|
* A validation function that takes data and returns the (possibly coerced)
|
|
7
7
|
* data or throws a validation error.
|
|
8
8
|
*/
|
|
9
|
-
export
|
|
10
|
-
export
|
|
9
|
+
export type Validator<T = any, R = T> = (data: T) => Promise<R>;
|
|
10
|
+
export type JSONSchemaDefinition = JSONSchema & {
|
|
11
11
|
$id: string;
|
|
12
12
|
$async?: true;
|
|
13
13
|
properties?: {
|
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.35",
|
|
5
5
|
"homepage": "https://feathersjs.com",
|
|
6
6
|
"main": "lib/",
|
|
7
7
|
"types": "lib/",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
],
|
|
43
43
|
"scripts": {
|
|
44
44
|
"prepublish": "npm run compile",
|
|
45
|
-
"pack": "npm pack --pack-destination ../
|
|
45
|
+
"pack": "npm pack --pack-destination ../generators/test/build",
|
|
46
46
|
"compile": "shx rm -rf lib/ && tsc && npm run pack",
|
|
47
47
|
"mocha": "mocha --config ../../.mocharc.json --recursive test/**.test.ts test/**/*.test.ts",
|
|
48
48
|
"test": "npm run compile && npm run mocha"
|
|
@@ -54,23 +54,24 @@
|
|
|
54
54
|
"access": "public"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@feathersjs/commons": "^5.0.0-pre.
|
|
58
|
-
"@feathersjs/
|
|
59
|
-
"@feathersjs/
|
|
60
|
-
"@feathersjs/
|
|
57
|
+
"@feathersjs/adapter-commons": "^5.0.0-pre.35",
|
|
58
|
+
"@feathersjs/commons": "^5.0.0-pre.35",
|
|
59
|
+
"@feathersjs/errors": "^5.0.0-pre.35",
|
|
60
|
+
"@feathersjs/feathers": "^5.0.0-pre.35",
|
|
61
|
+
"@feathersjs/hooks": "^0.7.6",
|
|
61
62
|
"@types/json-schema": "^7.0.11",
|
|
62
|
-
"ajv": "^8.11.
|
|
63
|
+
"ajv": "^8.11.2",
|
|
63
64
|
"ajv-formats": "^2.1.1",
|
|
64
|
-
"json-schema-to-ts": "^2.
|
|
65
|
+
"json-schema-to-ts": "^2.6.2"
|
|
65
66
|
},
|
|
66
67
|
"devDependencies": {
|
|
67
|
-
"@feathersjs/memory": "^5.0.0-pre.
|
|
68
|
-
"@types/mocha": "^10.0.
|
|
69
|
-
"@types/node": "^18.11.
|
|
68
|
+
"@feathersjs/memory": "^5.0.0-pre.35",
|
|
69
|
+
"@types/mocha": "^10.0.1",
|
|
70
|
+
"@types/node": "^18.11.18",
|
|
70
71
|
"ajv-formats": "^2.1.1",
|
|
71
|
-
"mocha": "^10.
|
|
72
|
+
"mocha": "^10.2.0",
|
|
72
73
|
"shx": "^0.3.4",
|
|
73
|
-
"typescript": "^4.
|
|
74
|
+
"typescript": "^4.9.4"
|
|
74
75
|
},
|
|
75
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "c641598d9a4de3ceda10f56cf2af288a4236b15e"
|
|
76
77
|
}
|
package/src/hooks/resolve.ts
CHANGED
|
@@ -78,21 +78,28 @@ export const resolveData =
|
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
export const resolveResult =
|
|
82
|
-
|
|
83
|
-
|
|
81
|
+
export const resolveResult = <T, H extends HookContext>(...resolvers: Resolver<T, H>[]) => {
|
|
82
|
+
const virtualProperties = new Set(
|
|
83
|
+
resolvers.reduce((acc, current) => acc.concat(current.virtualNames), [] as (keyof T)[])
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
return async (context: H, next?: NextFunction) => {
|
|
84
87
|
if (typeof next === 'function') {
|
|
85
|
-
const { $resolve:
|
|
88
|
+
const { $resolve, $select: select, ...query } = context.params?.query || {}
|
|
89
|
+
const $select = Array.isArray(select) ? select.filter((name) => !virtualProperties.has(name)) : select
|
|
86
90
|
const resolve = {
|
|
87
91
|
originalContext: context,
|
|
88
92
|
...context.params.resolve,
|
|
89
|
-
properties
|
|
93
|
+
properties: $resolve || select
|
|
90
94
|
}
|
|
91
95
|
|
|
92
96
|
context.params = {
|
|
93
97
|
...context.params,
|
|
94
98
|
resolve,
|
|
95
|
-
query
|
|
99
|
+
query: {
|
|
100
|
+
...query,
|
|
101
|
+
$select
|
|
102
|
+
}
|
|
96
103
|
}
|
|
97
104
|
|
|
98
105
|
await next()
|
|
@@ -112,6 +119,7 @@ export const resolveResult =
|
|
|
112
119
|
context.result = result
|
|
113
120
|
}
|
|
114
121
|
}
|
|
122
|
+
}
|
|
115
123
|
|
|
116
124
|
export const DISPATCH = Symbol('@feathersjs/schema/dispatch')
|
|
117
125
|
|
package/src/hooks/validate.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { HookContext, NextFunction } from '@feathersjs/feathers'
|
|
2
2
|
import { BadRequest } from '@feathersjs/errors'
|
|
3
|
+
import { VALIDATED } from '@feathersjs/adapter-commons'
|
|
3
4
|
import { Schema, Validator } from '../schema'
|
|
4
5
|
import { DataValidatorMap } from '../json-schema'
|
|
5
6
|
|
|
@@ -12,6 +13,8 @@ export const validateQuery = <H extends HookContext>(schema: Schema<any> | Valid
|
|
|
12
13
|
try {
|
|
13
14
|
const query = await validator(data)
|
|
14
15
|
|
|
16
|
+
Object.defineProperty(query, VALIDATED, { value: true })
|
|
17
|
+
|
|
15
18
|
context.params = {
|
|
16
19
|
...context.params,
|
|
17
20
|
query
|
|
@@ -26,12 +29,14 @@ export const validateQuery = <H extends HookContext>(schema: Schema<any> | Valid
|
|
|
26
29
|
}
|
|
27
30
|
}
|
|
28
31
|
|
|
29
|
-
export const validateData = <H extends HookContext>(schema: Schema<any> | DataValidatorMap) => {
|
|
32
|
+
export const validateData = <H extends HookContext>(schema: Schema<any> | DataValidatorMap | Validator) => {
|
|
30
33
|
return async (context: H, next?: NextFunction) => {
|
|
31
34
|
const data = context.data
|
|
32
35
|
const validator =
|
|
33
36
|
typeof (schema as Schema<any>).validate === 'function'
|
|
34
37
|
? (schema as Schema<any>).validate.bind(schema)
|
|
38
|
+
: typeof schema === 'function'
|
|
39
|
+
? schema
|
|
35
40
|
: (schema as any)[context.method]
|
|
36
41
|
|
|
37
42
|
if (validator) {
|
|
@@ -41,6 +46,8 @@ export const validateData = <H extends HookContext>(schema: Schema<any> | DataVa
|
|
|
41
46
|
} else {
|
|
42
47
|
context.data = await validator(data)
|
|
43
48
|
}
|
|
49
|
+
|
|
50
|
+
Object.defineProperty(context.data, VALIDATED, { value: true })
|
|
44
51
|
} catch (error: any) {
|
|
45
52
|
throw error.ajv ? new BadRequest(error.message, error.errors) : error
|
|
46
53
|
}
|
package/src/json-schema.ts
CHANGED
|
@@ -63,7 +63,7 @@ export const getDataValidator = (
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
export type PropertyQuery<D extends JSONSchema> = {
|
|
66
|
+
export type PropertyQuery<D extends JSONSchema, X> = {
|
|
67
67
|
anyOf: [
|
|
68
68
|
D,
|
|
69
69
|
{
|
|
@@ -83,7 +83,7 @@ export type PropertyQuery<D extends JSONSchema> = {
|
|
|
83
83
|
type: 'array'
|
|
84
84
|
items: D
|
|
85
85
|
}
|
|
86
|
-
}
|
|
86
|
+
} & X
|
|
87
87
|
}
|
|
88
88
|
]
|
|
89
89
|
}
|
|
@@ -92,9 +92,13 @@ export type PropertyQuery<D extends JSONSchema> = {
|
|
|
92
92
|
* Create a Feathers query syntax compatible JSON schema definition for a property definition.
|
|
93
93
|
*
|
|
94
94
|
* @param def The property definition (e.g. `{ type: 'string' }`)
|
|
95
|
+
* @param extensions Additional properties to add to the query property schema
|
|
95
96
|
* @returns A JSON schema definition for the Feathers query syntax for this property.
|
|
96
97
|
*/
|
|
97
|
-
export const queryProperty = <T extends JSONSchema
|
|
98
|
+
export const queryProperty = <T extends JSONSchema, X extends { [key: string]: JSONSchema }>(
|
|
99
|
+
def: T,
|
|
100
|
+
extensions: X = {} as X
|
|
101
|
+
) => {
|
|
98
102
|
const definition = _.omit(def, 'default')
|
|
99
103
|
return {
|
|
100
104
|
anyOf: [
|
|
@@ -115,7 +119,8 @@ export const queryProperty = <T extends JSONSchema>(def: T) => {
|
|
|
115
119
|
$nin: {
|
|
116
120
|
type: 'array',
|
|
117
121
|
items: definition
|
|
118
|
-
}
|
|
122
|
+
},
|
|
123
|
+
...extensions
|
|
119
124
|
}
|
|
120
125
|
}
|
|
121
126
|
]
|
|
@@ -126,27 +131,49 @@ export const queryProperty = <T extends JSONSchema>(def: T) => {
|
|
|
126
131
|
* Creates Feathers a query syntax compatible JSON schema for multiple properties.
|
|
127
132
|
*
|
|
128
133
|
* @param definitions A map of property definitions
|
|
134
|
+
* @param extensions Additional properties to add to the query property schema
|
|
129
135
|
* @returns The JSON schema definition for the Feathers query syntax for multiple properties
|
|
130
136
|
*/
|
|
131
|
-
export const queryProperties = <
|
|
137
|
+
export const queryProperties = <
|
|
138
|
+
T extends { [key: string]: JSONSchema },
|
|
139
|
+
X extends { [K in keyof T]?: { [key: string]: JSONSchema } }
|
|
140
|
+
>(
|
|
141
|
+
definitions: T,
|
|
142
|
+
extensions: X = {} as X
|
|
143
|
+
) =>
|
|
132
144
|
Object.keys(definitions).reduce((res, key) => {
|
|
133
145
|
const result = res as any
|
|
134
146
|
const definition = definitions[key]
|
|
147
|
+
const { $ref } = definition as any
|
|
148
|
+
|
|
149
|
+
if ($ref) {
|
|
150
|
+
throw new Error(`Can not create query syntax schema for reference property '${key}'`)
|
|
151
|
+
}
|
|
135
152
|
|
|
136
|
-
result[key] = queryProperty(definition)
|
|
153
|
+
result[key] = queryProperty(definition as JSONSchemaDefinition, extensions[key as keyof T])
|
|
137
154
|
|
|
138
155
|
return result
|
|
139
|
-
}, {} as { [K in keyof T]: PropertyQuery<T[K]> })
|
|
156
|
+
}, {} as { [K in keyof T]: PropertyQuery<T[K], X[K]> })
|
|
140
157
|
|
|
141
158
|
/**
|
|
142
159
|
* Creates a JSON schema for the complete Feathers query syntax including `$limit`, $skip`
|
|
143
160
|
* and `$sort` and `$select` for the allowed properties.
|
|
144
161
|
*
|
|
145
162
|
* @param definition The property definitions to create the query syntax schema for
|
|
163
|
+
* @param extensions Additional properties to add to the query property schema
|
|
146
164
|
* @returns A JSON schema for the complete query syntax
|
|
147
165
|
*/
|
|
148
|
-
export const querySyntax = <
|
|
149
|
-
|
|
166
|
+
export const querySyntax = <
|
|
167
|
+
T extends { [key: string]: JSONSchema },
|
|
168
|
+
X extends { [K in keyof T]?: { [key: string]: JSONSchema } }
|
|
169
|
+
>(
|
|
170
|
+
definition: T,
|
|
171
|
+
extensions: X = {} as X
|
|
172
|
+
) => {
|
|
173
|
+
const keys = Object.keys(definition)
|
|
174
|
+
const props = queryProperties(definition, extensions)
|
|
175
|
+
|
|
176
|
+
return {
|
|
150
177
|
$limit: {
|
|
151
178
|
type: 'number',
|
|
152
179
|
minimum: 0
|
|
@@ -157,7 +184,7 @@ export const querySyntax = <T extends { [key: string]: any }>(definition: T) =>
|
|
|
157
184
|
},
|
|
158
185
|
$sort: {
|
|
159
186
|
type: 'object',
|
|
160
|
-
properties:
|
|
187
|
+
properties: keys.reduce((res, key) => {
|
|
161
188
|
const result = res as any
|
|
162
189
|
|
|
163
190
|
result[key] = {
|
|
@@ -170,10 +197,20 @@ export const querySyntax = <T extends { [key: string]: any }>(definition: T) =>
|
|
|
170
197
|
},
|
|
171
198
|
$select: {
|
|
172
199
|
type: 'array',
|
|
200
|
+
maxItems: keys.length,
|
|
173
201
|
items: {
|
|
174
202
|
type: 'string',
|
|
175
|
-
enum:
|
|
203
|
+
...(keys.length > 0 ? { enum: keys as any as (keyof T)[] } : {})
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
$or: {
|
|
207
|
+
type: 'array',
|
|
208
|
+
items: {
|
|
209
|
+
type: 'object',
|
|
210
|
+
additionalProperties: false,
|
|
211
|
+
properties: props
|
|
176
212
|
}
|
|
177
213
|
},
|
|
178
|
-
...
|
|
179
|
-
} as const
|
|
214
|
+
...props
|
|
215
|
+
} as const
|
|
216
|
+
}
|
package/src/resolver.ts
CHANGED
|
@@ -1,15 +1,39 @@
|
|
|
1
1
|
import { BadRequest } from '@feathersjs/errors'
|
|
2
2
|
import { Schema } from './schema'
|
|
3
3
|
|
|
4
|
-
export type PropertyResolver<T, V, C> = (
|
|
4
|
+
export type PropertyResolver<T, V, C> = ((
|
|
5
5
|
value: V | undefined,
|
|
6
6
|
obj: T,
|
|
7
7
|
context: C,
|
|
8
8
|
status: ResolverStatus<T, C>
|
|
9
|
+
) => Promise<V | undefined>) & { [IS_VIRTUAL]?: boolean }
|
|
10
|
+
|
|
11
|
+
export type VirtualResolver<T, V, C> = (
|
|
12
|
+
obj: T,
|
|
13
|
+
context: C,
|
|
14
|
+
status: ResolverStatus<T, C>
|
|
9
15
|
) => Promise<V | undefined>
|
|
10
16
|
|
|
17
|
+
export const IS_VIRTUAL = Symbol('@feathersjs/schema/virtual')
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create a resolver for a virtual property. A virtual property is a property that
|
|
21
|
+
* is computed and never has an initial value.
|
|
22
|
+
*
|
|
23
|
+
* @param virtualResolver The virtual resolver function
|
|
24
|
+
* @returns The property resolver function
|
|
25
|
+
*/
|
|
26
|
+
export const virtual = <T, V, C>(virtualResolver: VirtualResolver<T, V, C>) => {
|
|
27
|
+
const propertyResolver: PropertyResolver<T, V, C> = async (_value, obj, context, status) =>
|
|
28
|
+
virtualResolver(obj, context, status)
|
|
29
|
+
|
|
30
|
+
propertyResolver[IS_VIRTUAL] = true
|
|
31
|
+
|
|
32
|
+
return propertyResolver
|
|
33
|
+
}
|
|
34
|
+
|
|
11
35
|
export type PropertyResolverMap<T, C> = {
|
|
12
|
-
[key in keyof T]?: PropertyResolver<T, T[key], C>
|
|
36
|
+
[key in keyof T]?: PropertyResolver<T, T[key], C> | ReturnType<typeof virtual<T, T[key], C>>
|
|
13
37
|
}
|
|
14
38
|
|
|
15
39
|
export type ResolverConverter<T, C> = (
|
|
@@ -18,8 +42,16 @@ export type ResolverConverter<T, C> = (
|
|
|
18
42
|
status: ResolverStatus<T, C>
|
|
19
43
|
) => Promise<T | undefined>
|
|
20
44
|
|
|
21
|
-
export interface
|
|
45
|
+
export interface ResolverOptions<T, C> {
|
|
22
46
|
schema?: Schema<T>
|
|
47
|
+
/**
|
|
48
|
+
* A converter function that is run before property resolvers
|
|
49
|
+
* to transform the initial data into a different format.
|
|
50
|
+
*/
|
|
51
|
+
converter?: ResolverConverter<T, C>
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface ResolverConfig<T, C> extends ResolverOptions<T, C> {
|
|
23
55
|
/**
|
|
24
56
|
* @deprecated Use the `validateData` and `validateQuery` hooks explicitly instead
|
|
25
57
|
*/
|
|
@@ -28,11 +60,6 @@ export interface ResolverConfig<T, C> {
|
|
|
28
60
|
* The properties to resolve
|
|
29
61
|
*/
|
|
30
62
|
properties: PropertyResolverMap<T, C>
|
|
31
|
-
/**
|
|
32
|
-
* A converter function that is run before property resolvers
|
|
33
|
-
* to transform the initial data into a different format.
|
|
34
|
-
*/
|
|
35
|
-
converter?: ResolverConverter<T, C>
|
|
36
63
|
}
|
|
37
64
|
|
|
38
65
|
export interface ResolverStatus<T, C> {
|
|
@@ -44,10 +71,12 @@ export interface ResolverStatus<T, C> {
|
|
|
44
71
|
|
|
45
72
|
export class Resolver<T, C> {
|
|
46
73
|
readonly _type!: T
|
|
47
|
-
|
|
74
|
+
public propertyNames: (keyof T)[]
|
|
75
|
+
public virtualNames: (keyof T)[]
|
|
48
76
|
|
|
49
|
-
constructor(public options: ResolverConfig<T, C>) {
|
|
50
|
-
this.propertyNames = Object.keys(options.properties)
|
|
77
|
+
constructor(public readonly options: ResolverConfig<T, C>) {
|
|
78
|
+
this.propertyNames = Object.keys(options.properties) as any as (keyof T)[]
|
|
79
|
+
this.virtualNames = this.propertyNames.filter((name) => options.properties[name][IS_VIRTUAL])
|
|
51
80
|
}
|
|
52
81
|
|
|
53
82
|
/**
|
|
@@ -106,7 +135,7 @@ export class Resolver<T, C> {
|
|
|
106
135
|
Array.isArray(status?.properties)
|
|
107
136
|
? status?.properties
|
|
108
137
|
: // By default get all data and resolver keys but remove duplicates
|
|
109
|
-
[...new Set(Object.keys(data).concat(this.propertyNames))]
|
|
138
|
+
[...new Set(Object.keys(data).concat(this.propertyNames as string[]))]
|
|
110
139
|
) as (keyof T)[]
|
|
111
140
|
|
|
112
141
|
const result: any = {}
|
|
@@ -155,6 +184,18 @@ export class Resolver<T, C> {
|
|
|
155
184
|
* @param options The configuration for the returned resolver
|
|
156
185
|
* @returns A new resolver instance
|
|
157
186
|
*/
|
|
158
|
-
export function resolve<T, C>(
|
|
159
|
-
|
|
187
|
+
export function resolve<T, C>(
|
|
188
|
+
properties: PropertyResolverMap<T, C>,
|
|
189
|
+
options?: ResolverOptions<T, C>
|
|
190
|
+
): Resolver<T, C>
|
|
191
|
+
export function resolve<T, C>(options: ResolverConfig<T, C>): Resolver<T, C>
|
|
192
|
+
export function resolve<T, C>(
|
|
193
|
+
properties: PropertyResolverMap<T, C> | ResolverConfig<T, C>,
|
|
194
|
+
options?: ResolverOptions<T, C>
|
|
195
|
+
) {
|
|
196
|
+
const settings = (
|
|
197
|
+
(properties as ResolverConfig<T, C>).properties ? properties : { properties, ...options }
|
|
198
|
+
) as ResolverConfig<T, C>
|
|
199
|
+
|
|
200
|
+
return new Resolver<T, C>(settings)
|
|
160
201
|
}
|