@feathersjs/typebox 5.0.0-pre.30 → 5.0.0-pre.32

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,16 @@
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.32](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.31...v5.0.0-pre.32) (2022-10-26)
7
+
8
+ **Note:** Version bump only for package @feathersjs/typebox
9
+
10
+ # [5.0.0-pre.31](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.30...v5.0.0-pre.31) (2022-10-12)
11
+
12
+ ### Features
13
+
14
+ - **cli:** Improve generated schema definitions ([#2783](https://github.com/feathersjs/feathers/issues/2783)) ([474a9fd](https://github.com/feathersjs/feathers/commit/474a9fda2107e9bcf357746320a8e00cda8182b6))
15
+
6
16
  # [5.0.0-pre.30](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.29...v5.0.0-pre.30) (2022-10-07)
7
17
 
8
18
  ### Features
package/lib/index.d.ts CHANGED
@@ -1,6 +1,31 @@
1
1
  import { TObject, TInteger, TOptional, TSchema, TIntersect } from '@sinclair/typebox';
2
+ import { Validator, DataValidatorMap, Ajv } from '@feathersjs/schema';
2
3
  export * from '@sinclair/typebox';
3
4
  export * from './default-schemas';
5
+ export declare type TDataSchemaMap = {
6
+ create: TObject;
7
+ update?: TObject;
8
+ patch?: TObject;
9
+ };
10
+ /**
11
+ * Returns a compiled validation function for a TypeBox object and AJV validator instance.
12
+ *
13
+ * @param schema The JSON schema definition
14
+ * @param validator The AJV validation instance
15
+ * @returns A compiled validation function
16
+ */
17
+ export declare const getValidator: <T = any, R = T>(schema: TObject, validator: Ajv) => Validator<T, R>;
18
+ /**
19
+ * Returns compiled validation functions to validate data for the `create`, `update` and `patch`
20
+ * service methods. If not passed explicitly, the `update` validator will be the same as the `create`
21
+ * and `patch` will be the `create` validator with no required fields.
22
+ *
23
+ * @param def Either general TypeBox object definition or a mapping of `create`, `update` and `patch`
24
+ * to their respective type object
25
+ * @param validator The Ajv instance to use as the validator
26
+ * @returns A map of validator functions
27
+ */
28
+ export declare const getDataValidator: (def: TObject | TDataSchemaMap, validator: Ajv) => DataValidatorMap;
4
29
  export declare function sortDefinition<T extends TObject>(schema: T): TObject<T["properties"] extends infer T_1 ? { [K in keyof T_1]: TOptional<TInteger>; } : never>;
5
30
  export declare const queryProperty: <T extends TSchema>(def: T) => TOptional<import("@sinclair/typebox").TUnion<[T, TObject<{
6
31
  $gt: TOptional<T>;
@@ -20,6 +45,13 @@ export declare const queryProperties: <T extends TObject<import("@sinclair/typeb
20
45
  $in: TOptional<import("@sinclair/typebox").TArray<T["properties"][K]>>;
21
46
  $nin: TOptional<import("@sinclair/typebox").TArray<T["properties"][K]>>;
22
47
  }>]>>; } : never>;
48
+ /**
49
+ * Creates a TypeBox schema for the complete Feathers query syntax including `$limit`, $skip`
50
+ * and `$sort` and `$select` for the allowed properties.
51
+ *
52
+ * @param type The properties to create the query syntax for
53
+ * @returns A TypeBox object representing the complete Feathers query syntax for the given properties
54
+ */
23
55
  export declare const querySyntax: <T extends TObject<import("@sinclair/typebox").TProperties> | TIntersect<TObject<import("@sinclair/typebox").TProperties>[]>>(type: T) => TIntersect<[TObject<{
24
56
  $limit: TOptional<import("@sinclair/typebox").TNumber>;
25
57
  $skip: TOptional<import("@sinclair/typebox").TNumber>;
package/lib/index.js CHANGED
@@ -14,10 +14,32 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.querySyntax = exports.queryProperties = exports.queryProperty = exports.sortDefinition = void 0;
17
+ exports.querySyntax = exports.queryProperties = exports.queryProperty = exports.sortDefinition = exports.getDataValidator = exports.getValidator = void 0;
18
18
  const typebox_1 = require("@sinclair/typebox");
19
+ const schema_1 = require("@feathersjs/schema");
19
20
  __exportStar(require("@sinclair/typebox"), exports);
20
21
  __exportStar(require("./default-schemas"), exports);
22
+ /**
23
+ * Returns a compiled validation function for a TypeBox object and AJV validator instance.
24
+ *
25
+ * @param schema The JSON schema definition
26
+ * @param validator The AJV validation instance
27
+ * @returns A compiled validation function
28
+ */
29
+ const getValidator = (schema, validator) => schema_1.jsonSchema.getValidator(schema, validator);
30
+ exports.getValidator = getValidator;
31
+ /**
32
+ * Returns compiled validation functions to validate data for the `create`, `update` and `patch`
33
+ * service methods. If not passed explicitly, the `update` validator will be the same as the `create`
34
+ * and `patch` will be the `create` validator with no required fields.
35
+ *
36
+ * @param def Either general TypeBox object definition or a mapping of `create`, `update` and `patch`
37
+ * to their respective type object
38
+ * @param validator The Ajv instance to use as the validator
39
+ * @returns A map of validator functions
40
+ */
41
+ const getDataValidator = (def, validator) => schema_1.jsonSchema.getDataValidator(def, validator);
42
+ exports.getDataValidator = getDataValidator;
21
43
  const arrayOfKeys = (type) => {
22
44
  const keys = Object.keys(type.properties);
23
45
  return typebox_1.Type.Unsafe({ type: 'array', items: { type: 'string', enum: keys } });
@@ -63,6 +85,13 @@ const queryProperties = (type) => {
63
85
  };
64
86
  };
65
87
  exports.queryProperties = queryProperties;
88
+ /**
89
+ * Creates a TypeBox schema for the complete Feathers query syntax including `$limit`, $skip`
90
+ * and `$sort` and `$select` for the allowed properties.
91
+ *
92
+ * @param type The properties to create the query syntax for
93
+ * @returns A TypeBox object representing the complete Feathers query syntax for the given properties
94
+ */
66
95
  const querySyntax = (type) => {
67
96
  return typebox_1.Type.Intersect([
68
97
  typebox_1.Type.Object({
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,+CAA2F;AAE3F,oDAAiC;AACjC,oDAAi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CAAC,GAAG,CAAC,CAAC,CAAA;QAEjD,OAAO,MAAM,CAAA;IACf,CAAC,EAAE,EAAyE,CAAC,CAAA;IAE7E,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,oBAAoB,EAAE,KAAK;QAC3B,UAAU;KACmB,CAAA;AACjC,CAAC,CAAA;AAdY,QAAA,eAAe,mBAc3B;AAED;;;;;;GAMG;AACI,MAAM,WAAW,GAAG,CAAiC,IAAO,EAAE,EAAE;IACrE,OAAO,cAAI,CAAC,SAAS,CAAC;QACpB,cAAI,CAAC,MAAM,CACT;YACE,MAAM,EAAE,cAAI,CAAC,QAAQ,CAAC,cAAI,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC;YAClD,KAAK,EAAE,cAAI,CAAC,QAAQ,CAAC,cAAI,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC;YACjD,KAAK,EAAE,cAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;YAC1C,OAAO,EAAE,cAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;SAC1C,EACD,EAAE,oBAAoB,EAAE,KAAK,EAAE,CAChC;QACD,IAAA,uBAAe,EAAC,IAAI,CAAC;KACtB,CAAC,CAAA;AACJ,CAAC,CAAA;AAbY,QAAA,WAAW,eAavB"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@feathersjs/typebox",
3
3
  "description": "TypeBox integration for @feathersjs/schema",
4
- "version": "5.0.0-pre.30",
4
+ "version": "5.0.0-pre.32",
5
5
  "homepage": "https://feathersjs.com",
6
6
  "main": "lib/",
7
7
  "types": "lib/",
@@ -54,15 +54,15 @@
54
54
  "access": "public"
55
55
  },
56
56
  "dependencies": {
57
+ "@feathersjs/schema": "^5.0.0-pre.32",
57
58
  "@sinclair/typebox": "^0.24.44"
58
59
  },
59
60
  "devDependencies": {
60
- "@feathersjs/schema": "^5.0.0-pre.30",
61
61
  "@types/mocha": "^10.0.0",
62
62
  "@types/node": "^18.8.2",
63
63
  "mocha": "^10.0.0",
64
64
  "shx": "^0.3.4",
65
65
  "typescript": "^4.8.4"
66
66
  },
67
- "gitHead": "b535c91197f4b997520e0a0e608793eeba791931"
67
+ "gitHead": "b405e205fc6d9fbd42b42c7a3f30ea4bf33d121d"
68
68
  }
package/src/index.ts CHANGED
@@ -1,8 +1,38 @@
1
1
  import { Type, TObject, TInteger, TOptional, TSchema, TIntersect } from '@sinclair/typebox'
2
+ import { jsonSchema, Validator, DataValidatorMap, Ajv } from '@feathersjs/schema'
2
3
 
3
4
  export * from '@sinclair/typebox'
4
5
  export * from './default-schemas'
5
6
 
7
+ export type TDataSchemaMap = {
8
+ create: TObject
9
+ update?: TObject
10
+ patch?: TObject
11
+ }
12
+
13
+ /**
14
+ * Returns a compiled validation function for a TypeBox object and AJV validator instance.
15
+ *
16
+ * @param schema The JSON schema definition
17
+ * @param validator The AJV validation instance
18
+ * @returns A compiled validation function
19
+ */
20
+ export const getValidator = <T = any, R = T>(schema: TObject, validator: Ajv): Validator<T, R> =>
21
+ jsonSchema.getValidator(schema as any, validator)
22
+
23
+ /**
24
+ * Returns compiled validation functions to validate data for the `create`, `update` and `patch`
25
+ * service methods. If not passed explicitly, the `update` validator will be the same as the `create`
26
+ * and `patch` will be the `create` validator with no required fields.
27
+ *
28
+ * @param def Either general TypeBox object definition or a mapping of `create`, `update` and `patch`
29
+ * to their respective type object
30
+ * @param validator The Ajv instance to use as the validator
31
+ * @returns A map of validator functions
32
+ */
33
+ export const getDataValidator = (def: TObject | TDataSchemaMap, validator: Ajv): DataValidatorMap =>
34
+ jsonSchema.getDataValidator(def as any, validator)
35
+
6
36
  const arrayOfKeys = <T extends TObject>(type: T) => {
7
37
  const keys = Object.keys(type.properties)
8
38
  return Type.Unsafe<(keyof T['properties'])[]>({ type: 'array', items: { type: 'string', enum: keys } })
@@ -59,6 +89,13 @@ export const queryProperties = <T extends TObject>(type: T) => {
59
89
  } as TObject<typeof properties>
60
90
  }
61
91
 
92
+ /**
93
+ * Creates a TypeBox schema for the complete Feathers query syntax including `$limit`, $skip`
94
+ * and `$sort` and `$select` for the allowed properties.
95
+ *
96
+ * @param type The properties to create the query syntax for
97
+ * @returns A TypeBox object representing the complete Feathers query syntax for the given properties
98
+ */
62
99
  export const querySyntax = <T extends TObject | TIntersect>(type: T) => {
63
100
  return Type.Intersect([
64
101
  Type.Object(