@feathersjs/schema 5.0.0-pre.32 → 5.0.0-pre.34
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 +19 -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 +15 -5
- package/lib/json-schema.js +48 -30
- 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 +14 -13
- package/src/hooks/resolve.ts +14 -6
- package/src/hooks/validate.ts +8 -1
- package/src/json-schema.ts +30 -6
- package/src/resolver.ts +55 -14
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,25 @@
|
|
|
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.34](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.33...v5.0.0-pre.34) (2022-12-14)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **core:** `context.type` for around hooks ([#2890](https://github.com/feathersjs/feathers/issues/2890)) ([d606ac6](https://github.com/feathersjs/feathers/commit/d606ac660fd5335c95206784fea36530dd2e851a))
|
|
11
|
+
- **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))
|
|
12
|
+
- **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))
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
|
|
16
|
+
- **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))
|
|
17
|
+
- **cli:** Use separate patch schema and types ([#2916](https://github.com/feathersjs/feathers/issues/2916)) ([7088af6](https://github.com/feathersjs/feathers/commit/7088af64a539dc7f1a016d832b77b98aaaf92603))
|
|
18
|
+
- **schema:** Split resolver options and property resolvers ([#2889](https://github.com/feathersjs/feathers/issues/2889)) ([4822c94](https://github.com/feathersjs/feathers/commit/4822c949812e5a1dceff3c62b2f9de4781b4d601))
|
|
19
|
+
- **schema:** Virtual property resolvers ([#2900](https://github.com/feathersjs/feathers/issues/2900)) ([7d03b57](https://github.com/feathersjs/feathers/commit/7d03b57ae2f633bdd4a368e0d5955011fbd6c329))
|
|
20
|
+
|
|
21
|
+
# [5.0.0-pre.33](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.32...v5.0.0-pre.33) (2022-11-08)
|
|
22
|
+
|
|
23
|
+
**Note:** Version bump only for package @feathersjs/schema
|
|
24
|
+
|
|
6
25
|
# [5.0.0-pre.32](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.31...v5.0.0-pre.32) (2022-10-26)
|
|
7
26
|
|
|
8
27
|
### Bug Fixes
|
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,MAAM,YAAY,GAAG,KAAK,EACxB,SAA2B,EAC3B,IAAS,EACT,GAAM,EACN,MAAsC,EACtC,EAAE;IACF,IAAI,OAAO,GAAQ,IAAI,CAAA;IAEvB,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE;QAChC,IAAI,QAAQ,IAAI,OAAO,QAAQ,CAAC,OAAO,KAAK,UAAU,EAAE;YACtD,OAAO,GAAG,MAAM,QAAQ,CAAC,OAAO,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AA;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,EAAE,EAAE;;QAC/C,MAAM,IAAI,GAAG,CAAA,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,0CAAE,KAAK,KAAI,EAAE,CAAA;QAEzC,IAAI;YACF,MAAM,KAAK,GAAG,MAAM,SAAS,CAAC,IAAI,CAAC,CAAA;YAEnC,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,2BAAS,EAAE,EAAE,KAAK,EAAE,IAA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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> = {
|
|
33
33
|
anyOf: [
|
|
34
34
|
D,
|
|
35
35
|
{
|
|
@@ -80,6 +80,7 @@ export declare const queryProperty: <T extends import("json-schema-to-ts").JSONS
|
|
|
80
80
|
};
|
|
81
81
|
}];
|
|
82
82
|
};
|
|
83
|
+
export declare const SUPPORTED_TYPES: string[];
|
|
83
84
|
/**
|
|
84
85
|
* Creates Feathers a query syntax compatible JSON schema for multiple properties.
|
|
85
86
|
*
|
|
@@ -97,7 +98,7 @@ export declare const queryProperties: <T extends {
|
|
|
97
98
|
* @returns A JSON schema for the complete query syntax
|
|
98
99
|
*/
|
|
99
100
|
export declare const querySyntax: <T extends {
|
|
100
|
-
[key: string]:
|
|
101
|
+
[key: string]: import("json-schema-to-ts").JSONSchema7;
|
|
101
102
|
}>(definition: T) => {
|
|
102
103
|
readonly $limit: {
|
|
103
104
|
readonly type: "number";
|
|
@@ -116,9 +117,18 @@ export declare const querySyntax: <T extends {
|
|
|
116
117
|
};
|
|
117
118
|
readonly $select: {
|
|
118
119
|
readonly type: "array";
|
|
120
|
+
readonly maxItems: number;
|
|
119
121
|
readonly items: {
|
|
122
|
+
readonly enum?: (keyof T)[];
|
|
120
123
|
readonly type: "string";
|
|
121
|
-
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
readonly $or: {
|
|
127
|
+
readonly type: "array";
|
|
128
|
+
readonly items: {
|
|
129
|
+
readonly type: "object";
|
|
130
|
+
readonly additionalProperties: false;
|
|
131
|
+
readonly properties: { [K_1 in keyof T]: PropertyQuery<T[K_1]>; };
|
|
122
132
|
};
|
|
123
133
|
};
|
|
124
134
|
} & { [K_1 in keyof T]: PropertyQuery<T[K_1]>; };
|
package/lib/json-schema.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.querySyntax = exports.queryProperties = exports.queryProperty = exports.getDataValidator = exports.getValidator = void 0;
|
|
3
|
+
exports.querySyntax = exports.queryProperties = exports.SUPPORTED_TYPES = exports.queryProperty = exports.getDataValidator = exports.getValidator = void 0;
|
|
4
4
|
const commons_1 = require("@feathersjs/commons");
|
|
5
5
|
/**
|
|
6
6
|
* Returns a compiled validation function for a schema and AJV validator instance.
|
|
@@ -74,6 +74,7 @@ const queryProperty = (def) => {
|
|
|
74
74
|
};
|
|
75
75
|
};
|
|
76
76
|
exports.queryProperty = queryProperty;
|
|
77
|
+
exports.SUPPORTED_TYPES = ['string', 'number', 'integer', 'boolean', 'null'];
|
|
77
78
|
/**
|
|
78
79
|
* Creates Feathers a query syntax compatible JSON schema for multiple properties.
|
|
79
80
|
*
|
|
@@ -83,6 +84,10 @@ exports.queryProperty = queryProperty;
|
|
|
83
84
|
const queryProperties = (definitions) => Object.keys(definitions).reduce((res, key) => {
|
|
84
85
|
const result = res;
|
|
85
86
|
const definition = definitions[key];
|
|
87
|
+
const { type, $ref } = definition;
|
|
88
|
+
if ($ref || !exports.SUPPORTED_TYPES.includes(type)) {
|
|
89
|
+
throw new Error(`Can not create query syntax schema for property '${key}'. Only types ${exports.SUPPORTED_TYPES.join(', ')} are allowed.`);
|
|
90
|
+
}
|
|
86
91
|
result[key] = (0, exports.queryProperty)(definition);
|
|
87
92
|
return result;
|
|
88
93
|
}, {});
|
|
@@ -94,34 +99,47 @@ exports.queryProperties = queryProperties;
|
|
|
94
99
|
* @param definition The property definitions to create the query syntax schema for
|
|
95
100
|
* @returns A JSON schema for the complete query syntax
|
|
96
101
|
*/
|
|
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
|
-
})
|
|
102
|
+
const querySyntax = (definition) => {
|
|
103
|
+
const keys = Object.keys(definition);
|
|
104
|
+
const props = (0, exports.queryProperties)(definition);
|
|
105
|
+
return {
|
|
106
|
+
$limit: {
|
|
107
|
+
type: 'number',
|
|
108
|
+
minimum: 0
|
|
109
|
+
},
|
|
110
|
+
$skip: {
|
|
111
|
+
type: 'number',
|
|
112
|
+
minimum: 0
|
|
113
|
+
},
|
|
114
|
+
$sort: {
|
|
115
|
+
type: 'object',
|
|
116
|
+
properties: keys.reduce((res, key) => {
|
|
117
|
+
const result = res;
|
|
118
|
+
result[key] = {
|
|
119
|
+
type: 'number',
|
|
120
|
+
enum: [1, -1]
|
|
121
|
+
};
|
|
122
|
+
return result;
|
|
123
|
+
}, {})
|
|
124
|
+
},
|
|
125
|
+
$select: {
|
|
126
|
+
type: 'array',
|
|
127
|
+
maxItems: keys.length,
|
|
128
|
+
items: {
|
|
129
|
+
type: 'string',
|
|
130
|
+
...(keys.length > 0 ? { enum: keys } : {})
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
$or: {
|
|
134
|
+
type: 'array',
|
|
135
|
+
items: {
|
|
136
|
+
type: 'object',
|
|
137
|
+
additionalProperties: false,
|
|
138
|
+
properties: props
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
...props
|
|
142
|
+
};
|
|
143
|
+
};
|
|
126
144
|
exports.querySyntax = querySyntax;
|
|
127
145
|
//# sourceMappingURL=json-schema.js.map
|
package/lib/json-schema.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json-schema.js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AAE,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;AA5CY,QAAA,WAAW,eA4CvB"}
|
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,kBAAU,CAAC,CAAC,CAAA;IAC/F,CAAC;IAED;;;;;;;;OAQG;IACH,KAAK,CAAC,eAAe,CACnB,IAAO,EACP,IAAO,EACP,OAAU,EACV,SAAwC,EAAE;QAE1C,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;QAC9C,MAAM,KAAK,GAAI,IAAY,CAAC,IAAI,CAAC,CAAA;QACjC,MAAM,EAAE,IAAI,GAAG,EAAE,EAAE,KA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AAA;oBAE3F,MAAM,CAAC,IAAI,CAAC,GAAG,cAAc,CAAA;oBAC7B,SAAS,GAAG,IAAI,CAAA;iBACjB;aACF;iBAAM,IAAI,KAAK,KAAK,SAAS,EAAE;gBAC9B,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,CAAA;aACrB;QACH,CAAC,CAAC,CACH,CAAA;QAED,IAAI,SAAS,EAAE;YACb,MAAM,YAAY,GAAG,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,UAAU,EAAC,CAAC,CAAC,IAAI,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAA;YAEhF,MAAM,IAAI,mBAAU,CAAC,sBAAsB,GAAG,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC,CAAA;SAChG;QAED,OAAO,MAAM,IAAI,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,MAAM,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAA;IAChF,CAAC;CACF;AA3GD,4BA2GC;AAaD,SAAgB,OAAO,CACrB,UAA4D,EAC5D,OAA+B;IAE/B,MAAM,QAAQ,GAAG,CACd,UAAmC,CAAC,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,GAAG,OAAO,EAAE,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.34",
|
|
5
5
|
"homepage": "https://feathersjs.com",
|
|
6
6
|
"main": "lib/",
|
|
7
7
|
"types": "lib/",
|
|
@@ -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.34",
|
|
58
|
+
"@feathersjs/commons": "^5.0.0-pre.34",
|
|
59
|
+
"@feathersjs/errors": "^5.0.0-pre.34",
|
|
60
|
+
"@feathersjs/feathers": "^5.0.0-pre.34",
|
|
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.1"
|
|
65
66
|
},
|
|
66
67
|
"devDependencies": {
|
|
67
|
-
"@feathersjs/memory": "^5.0.0-pre.
|
|
68
|
-
"@types/mocha": "^10.0.
|
|
69
|
-
"@types/node": "^18.
|
|
68
|
+
"@feathersjs/memory": "^5.0.0-pre.34",
|
|
69
|
+
"@types/mocha": "^10.0.1",
|
|
70
|
+
"@types/node": "^18.11.10",
|
|
70
71
|
"ajv-formats": "^2.1.1",
|
|
71
|
-
"mocha": "^10.
|
|
72
|
+
"mocha": "^10.1.0",
|
|
72
73
|
"shx": "^0.3.4",
|
|
73
|
-
"typescript": "^4.
|
|
74
|
+
"typescript": "^4.9.3"
|
|
74
75
|
},
|
|
75
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "42cca600d00f0b3b9d89fa79be30fcd46bc50132"
|
|
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
|
@@ -122,6 +122,8 @@ export const queryProperty = <T extends JSONSchema>(def: T) => {
|
|
|
122
122
|
} as const
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
+
export const SUPPORTED_TYPES = ['string', 'number', 'integer', 'boolean', 'null']
|
|
126
|
+
|
|
125
127
|
/**
|
|
126
128
|
* Creates Feathers a query syntax compatible JSON schema for multiple properties.
|
|
127
129
|
*
|
|
@@ -132,6 +134,15 @@ export const queryProperties = <T extends { [key: string]: JSONSchema }>(definit
|
|
|
132
134
|
Object.keys(definitions).reduce((res, key) => {
|
|
133
135
|
const result = res as any
|
|
134
136
|
const definition = definitions[key]
|
|
137
|
+
const { type, $ref } = definition as any
|
|
138
|
+
|
|
139
|
+
if ($ref || !SUPPORTED_TYPES.includes(type)) {
|
|
140
|
+
throw new Error(
|
|
141
|
+
`Can not create query syntax schema for property '${key}'. Only types ${SUPPORTED_TYPES.join(
|
|
142
|
+
', '
|
|
143
|
+
)} are allowed.`
|
|
144
|
+
)
|
|
145
|
+
}
|
|
135
146
|
|
|
136
147
|
result[key] = queryProperty(definition)
|
|
137
148
|
|
|
@@ -145,8 +156,11 @@ export const queryProperties = <T extends { [key: string]: JSONSchema }>(definit
|
|
|
145
156
|
* @param definition The property definitions to create the query syntax schema for
|
|
146
157
|
* @returns A JSON schema for the complete query syntax
|
|
147
158
|
*/
|
|
148
|
-
export const querySyntax = <T extends { [key: string]:
|
|
149
|
-
(
|
|
159
|
+
export const querySyntax = <T extends { [key: string]: JSONSchema }>(definition: T) => {
|
|
160
|
+
const keys = Object.keys(definition)
|
|
161
|
+
const props = queryProperties(definition)
|
|
162
|
+
|
|
163
|
+
return {
|
|
150
164
|
$limit: {
|
|
151
165
|
type: 'number',
|
|
152
166
|
minimum: 0
|
|
@@ -157,7 +171,7 @@ export const querySyntax = <T extends { [key: string]: any }>(definition: T) =>
|
|
|
157
171
|
},
|
|
158
172
|
$sort: {
|
|
159
173
|
type: 'object',
|
|
160
|
-
properties:
|
|
174
|
+
properties: keys.reduce((res, key) => {
|
|
161
175
|
const result = res as any
|
|
162
176
|
|
|
163
177
|
result[key] = {
|
|
@@ -170,10 +184,20 @@ export const querySyntax = <T extends { [key: string]: any }>(definition: T) =>
|
|
|
170
184
|
},
|
|
171
185
|
$select: {
|
|
172
186
|
type: 'array',
|
|
187
|
+
maxItems: keys.length,
|
|
173
188
|
items: {
|
|
174
189
|
type: 'string',
|
|
175
|
-
enum:
|
|
190
|
+
...(keys.length > 0 ? { enum: keys as any as (keyof T)[] } : {})
|
|
176
191
|
}
|
|
177
192
|
},
|
|
178
|
-
|
|
179
|
-
|
|
193
|
+
$or: {
|
|
194
|
+
type: 'array',
|
|
195
|
+
items: {
|
|
196
|
+
type: 'object',
|
|
197
|
+
additionalProperties: false,
|
|
198
|
+
properties: props
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
...props
|
|
202
|
+
} as const
|
|
203
|
+
}
|
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
|
}
|