@feathersjs/schema 5.0.0-pre.19 → 5.0.0-pre.20

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 CHANGED
@@ -3,6 +3,22 @@
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.20](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.19...v5.0.0-pre.20) (2022-05-04)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * **dependencies:** Lock monorepo package version numbers ([#2623](https://github.com/feathersjs/feathers/issues/2623)) ([5640c10](https://github.com/feathersjs/feathers/commit/5640c1020cc139994e695d658c08bad3494db507))
12
+
13
+
14
+ ### Features
15
+
16
+ * **schema:** Add querySyntax helper to create full query schemas ([#2621](https://github.com/feathersjs/feathers/issues/2621)) ([2bbb103](https://github.com/feathersjs/feathers/commit/2bbb103b2f3e30fb0fff935f92ad3276a1a67e41))
17
+
18
+
19
+
20
+
21
+
6
22
  # [5.0.0-pre.19](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.18...v5.0.0-pre.19) (2022-05-01)
7
23
 
8
24
 
package/lib/query.d.ts CHANGED
@@ -1,3 +1,28 @@
1
+ import { JSONSchema } from 'json-schema-to-ts';
2
+ export declare type PropertyQuery<D extends JSONSchema> = {
3
+ anyOf: [
4
+ D,
5
+ {
6
+ type: 'object';
7
+ additionalProperties: false;
8
+ properties: {
9
+ $gt: D;
10
+ $gte: D;
11
+ $lt: D;
12
+ $lte: D;
13
+ $ne: D;
14
+ $in: {
15
+ type: 'array';
16
+ items: D;
17
+ };
18
+ $nin: {
19
+ type: 'array';
20
+ items: D;
21
+ };
22
+ };
23
+ }
24
+ ];
25
+ };
1
26
  export declare const queryProperty: <T extends import("json-schema-to-ts").JSONSchema7>(definition: T) => {
2
27
  readonly anyOf: readonly [T, {
3
28
  readonly type: "object";
@@ -19,3 +44,32 @@ export declare const queryProperty: <T extends import("json-schema-to-ts").JSONS
19
44
  };
20
45
  }];
21
46
  };
47
+ export declare const queryProperties: <T extends {
48
+ [key: string]: import("json-schema-to-ts").JSONSchema7;
49
+ }>(definition: T) => { [K in keyof T]: PropertyQuery<T[K]>; };
50
+ export declare const querySyntax: <T extends {
51
+ [key: string]: import("json-schema-to-ts").JSONSchema7;
52
+ }>(definition: T) => {
53
+ readonly $limit: {
54
+ readonly type: "number";
55
+ readonly minimum: 0;
56
+ };
57
+ readonly $skip: {
58
+ readonly type: "number";
59
+ readonly minimum: 0;
60
+ };
61
+ readonly $sort: {
62
+ readonly type: "object";
63
+ readonly properties: { [K in keyof T]: {
64
+ readonly type: 'number';
65
+ readonly enum: [1, -1];
66
+ }; };
67
+ };
68
+ readonly $select: {
69
+ readonly type: "array";
70
+ readonly items: {
71
+ readonly type: "string";
72
+ readonly enum: (keyof T)[];
73
+ };
74
+ };
75
+ } & { [K_1 in keyof T]: PropertyQuery<T[K_1]>; };
package/lib/query.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.queryProperty = void 0;
3
+ exports.querySyntax = exports.queryProperties = exports.queryProperty = void 0;
4
4
  const queryProperty = (definition) => ({
5
5
  anyOf: [
6
6
  definition,
@@ -26,4 +26,38 @@ const queryProperty = (definition) => ({
26
26
  ]
27
27
  });
28
28
  exports.queryProperty = queryProperty;
29
+ const queryProperties = (definition) => Object.keys(definition).reduce((res, key) => {
30
+ res[key] = (0, exports.queryProperty)(definition[key]);
31
+ return res;
32
+ }, {});
33
+ exports.queryProperties = queryProperties;
34
+ const querySyntax = (definition) => ({
35
+ $limit: {
36
+ type: 'number',
37
+ minimum: 0
38
+ },
39
+ $skip: {
40
+ type: 'number',
41
+ minimum: 0
42
+ },
43
+ $sort: {
44
+ type: 'object',
45
+ properties: Object.keys(definition).reduce((res, key) => {
46
+ res[key] = {
47
+ type: 'number',
48
+ enum: [1, -1]
49
+ };
50
+ return res;
51
+ }, {})
52
+ },
53
+ $select: {
54
+ type: 'array',
55
+ items: {
56
+ type: 'string',
57
+ enum: Object.keys(definition)
58
+ }
59
+ },
60
+ ...(0, exports.queryProperties)(definition)
61
+ });
62
+ exports.querySyntax = querySyntax;
29
63
  //# 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":";;;AA2BO,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;gBAC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ib/schema.d.ts CHANGED
@@ -5,6 +5,10 @@ export { Ajv };
5
5
  export declare type JSONSchemaDefinition = JSONSchema & {
6
6
  $id: string;
7
7
  $async?: boolean;
8
+ properties?: {
9
+ [key: string]: JSONSchema;
10
+ };
11
+ required?: readonly string[];
8
12
  };
9
13
  export interface Schema<T> {
10
14
  validate<X = T>(...args: Parameters<ValidateFunction<X>>): Promise<X>;
@@ -15,6 +19,8 @@ export declare class SchemaWrapper<S extends JSONSchemaDefinition> implements Sc
15
19
  validator: AsyncValidateFunction;
16
20
  readonly _type: FromSchema<S>;
17
21
  constructor(definition: S, ajv?: Ajv);
22
+ get properties(): S["properties"];
23
+ get required(): S["required"];
18
24
  validate<T = FromSchema<S>>(...args: Parameters<ValidateFunction<T>>): Promise<T>;
19
25
  toJSON(): S;
20
26
  }
package/lib/schema.js CHANGED
@@ -19,6 +19,12 @@ class SchemaWrapper {
19
19
  ...this.definition
20
20
  });
21
21
  }
22
+ get properties() {
23
+ return this.definition.properties;
24
+ }
25
+ get required() {
26
+ return this.definition.required;
27
+ }
22
28
  async validate(...args) {
23
29
  try {
24
30
  const validated = await this.validator(...args);
package/lib/schema.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":";;;;;;AAAA,8CAAmE;AAQ1D,cARF,aAAG,CAQE;AANZ,+CAAgD;AAEnC,QAAA,WAAW,GAAG,IAAI,aAAG,CAAC;IACjC,WAAW,EAAE,IAAI;CAClB,CAAC,CAAC;AAUH,MAAa,aAAa;IAKxB,YAAoB,UAAa,EAAE,MAAW,mBAAW;QAArC,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,CA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M,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;AAlCD,sCAkCC;AAED,SAAgB,MAAM,CAAmC,UAAa,EAAE,MAAW,mBAAW;IAC5F,OAAO,IAAI,aAAa,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;AAC5C,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.19",
4
+ "version": "5.0.0-pre.20",
5
5
  "homepage": "https://feathersjs.com",
6
6
  "main": "lib/",
7
7
  "types": "lib/",
@@ -53,15 +53,15 @@
53
53
  "access": "public"
54
54
  },
55
55
  "dependencies": {
56
- "@feathersjs/errors": "^5.0.0-pre.19",
57
- "@feathersjs/feathers": "^5.0.0-pre.19",
56
+ "@feathersjs/errors": "^5.0.0-pre.20",
57
+ "@feathersjs/feathers": "^5.0.0-pre.20",
58
58
  "@types/json-schema": "^7.0.11",
59
59
  "ajv": "^8.11.0",
60
60
  "json-schema": "^0.4.0",
61
61
  "json-schema-to-ts": "^2.3.0"
62
62
  },
63
63
  "devDependencies": {
64
- "@feathersjs/memory": "^5.0.0-pre.19",
64
+ "@feathersjs/memory": "^5.0.0-pre.20",
65
65
  "@types/mocha": "^9.1.1",
66
66
  "@types/node": "^17.0.31",
67
67
  "ajv-formats": "^2.1.1",
@@ -69,5 +69,5 @@
69
69
  "shx": "^0.3.4",
70
70
  "typescript": "^4.6.4"
71
71
  },
72
- "gitHead": "57f3e18bb62735e1869ffafee90286498738fdfa"
72
+ "gitHead": "54de749a0b392c7da726c668002b50cafaca530c"
73
73
  }
package/src/query.ts CHANGED
@@ -1,5 +1,30 @@
1
1
  import { JSONSchema } from 'json-schema-to-ts';
2
2
 
3
+ export type PropertyQuery<D extends JSONSchema> = {
4
+ anyOf: [
5
+ D,
6
+ {
7
+ type: 'object',
8
+ additionalProperties: false,
9
+ properties: {
10
+ $gt: D,
11
+ $gte: D,
12
+ $lt: D,
13
+ $lte: D,
14
+ $ne: D,
15
+ $in: {
16
+ type: 'array',
17
+ items: D
18
+ },
19
+ $nin: {
20
+ type: 'array',
21
+ items: D
22
+ }
23
+ }
24
+ }
25
+ ]
26
+ }
27
+
3
28
  export const queryProperty = <T extends JSONSchema> (definition: T) => ({
4
29
  anyOf: [
5
30
  definition,
@@ -24,3 +49,40 @@ export const queryProperty = <T extends JSONSchema> (definition: T) => ({
24
49
  }
25
50
  ]
26
51
  } as const);
52
+
53
+ export const queryProperties = <T extends { [key: string]: JSONSchema }> (definition: T) =>
54
+ Object.keys(definition).reduce((res, key) => {
55
+ (res as any)[key] = queryProperty(definition[key])
56
+
57
+ return res
58
+ }, {} as { [K in keyof T]: PropertyQuery<T[K]> })
59
+
60
+ export const querySyntax = <T extends { [key: string]: JSONSchema }> (definition: T) => ({
61
+ $limit: {
62
+ type: 'number',
63
+ minimum: 0
64
+ },
65
+ $skip: {
66
+ type: 'number',
67
+ minimum: 0
68
+ },
69
+ $sort: {
70
+ type: 'object',
71
+ properties: Object.keys(definition).reduce((res, key) => {
72
+ (res as any)[key] = {
73
+ type: 'number',
74
+ enum: [1, -1]
75
+ }
76
+
77
+ return res
78
+ }, {} as { [K in keyof T]: { readonly type: 'number', readonly enum: [1, -1] } })
79
+ },
80
+ $select: {
81
+ type: 'array',
82
+ items: {
83
+ type: 'string',
84
+ enum: Object.keys(definition) as any as (keyof T)[]
85
+ }
86
+ },
87
+ ...queryProperties(definition)
88
+ } as const)
package/src/schema.ts CHANGED
@@ -8,7 +8,12 @@ export const DEFAULT_AJV = new Ajv({
8
8
 
9
9
  export { Ajv };
10
10
 
11
- export type JSONSchemaDefinition = JSONSchema & { $id: string, $async?: boolean };
11
+ export type JSONSchemaDefinition = JSONSchema & {
12
+ $id: string,
13
+ $async?: boolean,
14
+ properties?: { [key: string]: JSONSchema }
15
+ required?: readonly string[]
16
+ };
12
17
 
13
18
  export interface Schema<T> {
14
19
  validate <X = T> (...args: Parameters<ValidateFunction<X>>): Promise<X>;
@@ -27,6 +32,14 @@ export class SchemaWrapper<S extends JSONSchemaDefinition> implements Schema<Fro
27
32
  }) as AsyncValidateFunction;
28
33
  }
29
34
 
35
+ get properties () {
36
+ return this.definition.properties as S['properties'];
37
+ }
38
+
39
+ get required () {
40
+ return this.definition.required as S['required'];
41
+ }
42
+
30
43
  async validate <T = FromSchema<S>> (...args: Parameters<ValidateFunction<T>>) {
31
44
  try {
32
45
  const validated = await this.validator(...args) as T;