@feathersjs/schema 0.0.1-alpha.4 → 5.0.0-pre.15

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,43 +3,55 @@
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
- ## [0.0.1-alpha.4](https://github.com/feathersjs/schema/compare/v0.0.1-alpha.3...v0.0.1-alpha.4) (2020-02-08)
6
+ # [5.0.0-pre.15](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.14...v5.0.0-pre.15) (2021-11-27)
7
7
 
8
8
 
9
9
  ### Bug Fixes
10
10
 
11
- * Fix getting and resolving schemas from undefined objects ([#14](https://github.com/feathersjs/schema/issues/14)) ([511752e](https://github.com/feathersjs/schema/commit/511752e7992fc0100acd1189638a2bd8178abc70))
11
+ * **typescript:** Overall typing improvements ([#2478](https://github.com/feathersjs/feathers/issues/2478)) ([b8eb804](https://github.com/feathersjs/feathers/commit/b8eb804158556d9651a8607e3c3fda15e0bfd110))
12
12
 
13
13
 
14
14
 
15
15
 
16
16
 
17
- ## [0.0.1-alpha.2](https://github.com/feathersjs/schema/compare/v0.0.1-alpha.1...v0.0.1-alpha.2) (2019-12-18)
17
+ # [5.0.0-pre.14](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.13...v5.0.0-pre.14) (2021-10-13)
18
18
 
19
+ **Note:** Version bump only for package @feathersjs/schema
19
20
 
20
- ### Features
21
21
 
22
- * Add @feathersjs/schema-hooks ([#5](https://github.com/feathersjs/schema/issues/5)) ([09c3e9f](https://github.com/feathersjs/schema/commit/09c3e9f5c22103a805d473d0db5d7bf7dc7cee13))
22
+
23
+
24
+
25
+ # [5.0.0-pre.13](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.12...v5.0.0-pre.13) (2021-10-13)
26
+
27
+ **Note:** Version bump only for package @feathersjs/schema
28
+
29
+
30
+
31
+
32
+
33
+ # [5.0.0-pre.12](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.11...v5.0.0-pre.12) (2021-10-12)
34
+
35
+ **Note:** Version bump only for package @feathersjs/schema
23
36
 
24
37
 
25
38
 
26
39
 
27
40
 
28
- ## [0.0.1-alpha.1](https://github.com/feathersjs/schema/compare/v0.0.1-alpha.0...v0.0.1-alpha.1) (2019-12-13)
41
+ # [5.0.0-pre.11](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.10...v5.0.0-pre.11) (2021-10-06)
29
42
 
30
43
 
31
44
  ### Features
32
45
 
33
- * Allow references by name and prevent circular resolvers ([#3](https://github.com/feathersjs/schema/issues/3)) ([bb34edd](https://github.com/feathersjs/schema/commit/bb34edd36f47b375871ddbde86a089ae65391dda))
34
- * First cut of working Sequelize model converter ([#4](https://github.com/feathersjs/schema/issues/4)) ([3187896](https://github.com/feathersjs/schema/commit/3187896304cf32043f5b5b569e2976b986e5eca4))
46
+ * **schema:** Allow resolvers to validate a schema ([#2465](https://github.com/feathersjs/feathers/issues/2465)) ([7d9590b](https://github.com/feathersjs/feathers/commit/7d9590bbe12b94b8b5a7987684f5d4968e426481))
35
47
 
36
48
 
37
49
 
38
50
 
39
51
 
40
- ## 0.0.1-alpha.0 (2019-12-05)
52
+ # [5.0.0-pre.10](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.9...v5.0.0-pre.10) (2021-09-19)
41
53
 
42
54
 
43
55
  ### Features
44
56
 
45
- * First version of schema prototype ([#2](https://github.com/feathersjs/schema/issues/2)) ([e11f86e](https://github.com/feathersjs/schema/commit/e11f86e3a43f667e8c4bcb987fa5f917cbf156a5))
57
+ * **schema:** Initial version of schema definitions and resolvers ([#2441](https://github.com/feathersjs/feathers/issues/2441)) ([c57a5cd](https://github.com/feathersjs/feathers/commit/c57a5cd56699a121647be4506d8f967e6d72ecae))
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
- MIT License
1
+ The MIT License (MIT)
2
2
 
3
- Copyright (c) 2019 Feathers
3
+ Copyright (c) 2021 Feathers
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -19,3 +19,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # @feathersjs/schema
2
+
3
+ [![CI](https://github.com/feathersjs/feathers/workflows/CI/badge.svg)](https://github.com/feathersjs/feathers/actions?query=workflow%3ACI)
4
+ [![Dependency Status](https://img.shields.io/david/feathersjs/feathers.svg?style=flat-square&path=packages/socketio)](https://david-dm.org/feathersjs/feathers?path=packages/schema)
5
+ [![Download Status](https://img.shields.io/npm/dm/@feathersjs/schema.svg?style=flat-square)](https://www.npmjs.com/package/@feathersjs/schema)
6
+
7
+ > A common data schema definition format
8
+
9
+ ## Installation
10
+
11
+ ```
12
+ npm install @feathersjs/schema --save
13
+ ```
14
+
15
+ ## Documentation
16
+
17
+ Refer to the [Feathers documentation](https://docs.feathersjs.com) for more details.
18
+
19
+ ## License
20
+
21
+ Copyright (c) 2021 [Feathers contributors](https://github.com/feathersjs/feathers/graphs/contributors)
22
+
23
+ Licensed under the [MIT license](LICENSE).
package/lib/hooks.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { HookContext, NextFunction } from '@feathersjs/feathers';
2
+ import { Resolver } from './resolver';
3
+ import { Schema } from './schema';
4
+ export declare const resolveQuery: <T>(resolver: Resolver<T, HookContext<import("@feathersjs/feathers").Application<any, any>, any>>) => (context: HookContext, next: NextFunction) => Promise<any>;
5
+ export declare const resolveData: <T>(resolver: Resolver<T, HookContext<import("@feathersjs/feathers").Application<any, any>, any>>) => (context: HookContext, next: NextFunction) => Promise<any>;
6
+ export declare const resolveResult: <T>(resolver: Resolver<T, HookContext<import("@feathersjs/feathers").Application<any, any>, any>>) => (context: HookContext, next: NextFunction) => Promise<void>;
7
+ export declare const validateQuery: (schema: Schema<any>) => (context: HookContext, next: NextFunction) => Promise<any>;
8
+ export declare const validateData: (schema: Schema<any>) => (context: HookContext, next: NextFunction) => Promise<any>;
package/lib/hooks.js ADDED
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateData = exports.validateQuery = exports.resolveResult = exports.resolveData = exports.resolveQuery = void 0;
4
+ const lib_1 = require("../../errors/lib");
5
+ const getContext = (context) => {
6
+ return {
7
+ ...context,
8
+ params: {
9
+ ...context.params,
10
+ query: {}
11
+ }
12
+ };
13
+ };
14
+ const resolveQuery = (resolver) => async (context, next) => {
15
+ var _a;
16
+ const ctx = getContext(context);
17
+ const data = ((_a = context === null || context === void 0 ? void 0 : context.params) === null || _a === void 0 ? void 0 : _a.query) || {};
18
+ const query = await resolver.resolve(data, ctx, {
19
+ originalContext: context
20
+ });
21
+ context.params = {
22
+ ...context.params,
23
+ query
24
+ };
25
+ return next();
26
+ };
27
+ exports.resolveQuery = resolveQuery;
28
+ const resolveData = (resolver) => async (context, next) => {
29
+ const ctx = getContext(context);
30
+ const data = context.data;
31
+ const status = {
32
+ originalContext: context
33
+ };
34
+ if (Array.isArray(data)) {
35
+ context.data = await Promise.all(data.map(current => resolver.resolve(current, ctx, status)));
36
+ }
37
+ else {
38
+ context.data = await resolver.resolve(data, ctx, status);
39
+ }
40
+ return next();
41
+ };
42
+ exports.resolveData = resolveData;
43
+ const resolveResult = (resolver) => async (context, next) => {
44
+ var _a;
45
+ const { $resolve: properties, ...query } = ((_a = context.params) === null || _a === void 0 ? void 0 : _a.query) || {};
46
+ const { resolve } = context.params;
47
+ const status = {
48
+ originalContext: context,
49
+ ...resolve,
50
+ properties
51
+ };
52
+ context.params = {
53
+ ...context.params,
54
+ query
55
+ };
56
+ await next();
57
+ const ctx = getContext(context);
58
+ const data = context.method === 'find' && context.result.data
59
+ ? context.result.data
60
+ : context.result;
61
+ if (Array.isArray(data)) {
62
+ context.result = await Promise.all(data.map(current => resolver.resolve(current, ctx, status)));
63
+ }
64
+ else {
65
+ context.result = await resolver.resolve(data, ctx, status);
66
+ }
67
+ };
68
+ exports.resolveResult = resolveResult;
69
+ const validateQuery = (schema) => async (context, next) => {
70
+ var _a;
71
+ const data = ((_a = context === null || context === void 0 ? void 0 : context.params) === null || _a === void 0 ? void 0 : _a.query) || {};
72
+ try {
73
+ const query = await schema.validate(data);
74
+ context.params = {
75
+ ...context.params,
76
+ query
77
+ };
78
+ return next();
79
+ }
80
+ catch (error) {
81
+ throw (error.ajv ? new lib_1.BadRequest(error.message, error.errors) : error);
82
+ }
83
+ };
84
+ exports.validateQuery = validateQuery;
85
+ const validateData = (schema) => async (context, next) => {
86
+ const data = context.data;
87
+ try {
88
+ if (Array.isArray(data)) {
89
+ context.data = await Promise.all(data.map(current => schema.validate(current)));
90
+ }
91
+ else {
92
+ context.data = await schema.validate(data);
93
+ }
94
+ }
95
+ catch (error) {
96
+ throw (error.ajv ? new lib_1.BadRequest(error.message, error.errors) : error);
97
+ }
98
+ return next();
99
+ };
100
+ exports.validateData = validateData;
101
+ //# sourceMappingURL=hooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.js","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":";;;AACA,0CAA8C;AAI9C,MAAM,UAAU,GAAG,CAAC,OAAoB,EAAE,EAAE;IAC1C,OAAO;QACL,GAAG,OAAO;QACV,MAAM,EAAE;YACN,GAAG,OAAO,CAAC,MAAM;YACjB,KAAK,EAAE,EAAE;SACV;KACF,CAAA;AACH,CAAC,CAAA;AAEM,MAAM,YAAY,GAAG,CAAK,QAAkC,EAAE,EAAE,CACrE,KAAK,EAAE,OAAoB,EAAE,IAAkB,EAAE,EAAE;;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1,9 +1,13 @@
1
- import { SchemaMeta, SchemaProperties, Schema } from './core';
2
- import { propertyDecorator } from './decorator';
3
- export * from './core';
4
- export * from './decorator';
5
- export * from './resolve';
6
- export declare const property: typeof propertyDecorator;
7
- export declare function schema(schemaMeta: Partial<SchemaMeta>, schemaProperties: SchemaProperties): Schema;
8
- export declare function schema(target: any, schemaMeta: Partial<SchemaMeta>, schemaProperties: SchemaProperties): Schema;
9
- export declare function schema(schemaMeta: Partial<SchemaMeta>): (target: any) => void;
1
+ import { ResolverStatus } from './resolver';
2
+ export * from './schema';
3
+ export * from './resolver';
4
+ export * from './hooks';
5
+ export * from './query';
6
+ export declare type Infer<S extends {
7
+ _type: any;
8
+ }> = S['_type'];
9
+ declare module '@feathersjs/feathers/lib/declarations' {
10
+ interface Params {
11
+ resolve?: ResolverStatus<any, HookContext>;
12
+ }
13
+ }
package/lib/index.js CHANGED
@@ -1,23 +1,17 @@
1
1
  "use strict";
2
- function __export(m) {
3
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4
- }
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
5
12
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const core_1 = require("./core");
7
- const decorator_1 = require("./decorator");
8
- __export(require("./core"));
9
- __export(require("./decorator"));
10
- __export(require("./resolve"));
11
- exports.property = decorator_1.propertyDecorator;
12
- function schema(...args) {
13
- if (args.length === 1) {
14
- return decorator_1.schemaDecorator(args[0]);
15
- }
16
- const target = args.length === 3 ? args.shift() : null;
17
- const [schemaMeta, schemaProperties] = args;
18
- const schema = new core_1.Schema(schemaMeta, schemaProperties);
19
- core_1.setSchema(schema, target);
20
- return schema;
21
- }
22
- exports.schema = schema;
13
+ __exportStar(require("./schema"), exports);
14
+ __exportStar(require("./resolver"), exports);
15
+ __exportStar(require("./hooks"), exports);
16
+ __exportStar(require("./query"), exports);
23
17
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,iCAAyE;AACzE,2CAAiE;AAEjE,4BAAuB;AACvB,iCAA4B;AAC5B,+BAA0B;AAEb,QAAA,QAAQ,GAAG,6BAAiB,CAAC;AAK1C,SAAgB,MAAM,CAAE,GAAG,IAAW;IACpC,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;QACrB,OAAO,2BAAe,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;KACjC;IAED,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IACvD,MAAM,CAAE,UAAU,EAAE,gBAAgB,CAAE,GAAG,IAAI,CAAC;IAC9C,MAAM,MAAM,GAAG,IAAI,aAAM,CAAC,UAAU,EAAE,gBAAgB,CAAC,CAAC;IAExD,gBAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE1B,OAAO,MAAM,CAAC;AAChB,CAAC;AAZD,wBAYC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,2CAAyB;AACzB,6CAA2B;AAC3B,0CAAwB;AACxB,0CAAwB"}
package/lib/query.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { JSONSchema } from 'json-schema-to-ts';
2
+ export declare const queryProperty: <T extends JSONSchema>(definition: T) => {
3
+ readonly oneOf: readonly [T, {
4
+ readonly type: "object";
5
+ readonly additionalProperties: false;
6
+ readonly properties: {
7
+ readonly $gt: T;
8
+ readonly $gte: T;
9
+ readonly $lt: T;
10
+ readonly $lte: T;
11
+ readonly $ne: T;
12
+ readonly $in: {
13
+ readonly type: "array";
14
+ readonly items: T;
15
+ };
16
+ readonly $nin: {
17
+ readonly type: "array";
18
+ readonly items: T;
19
+ };
20
+ };
21
+ }];
22
+ };
23
+ export declare const queryArray: <T extends readonly string[]>(fields: T) => {
24
+ readonly type: "array";
25
+ readonly items: {
26
+ readonly type: "string";
27
+ readonly enum: T;
28
+ };
29
+ };
package/lib/query.js ADDED
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.queryArray = exports.queryProperty = void 0;
4
+ const queryProperty = (definition) => ({
5
+ oneOf: [
6
+ definition,
7
+ {
8
+ type: 'object',
9
+ additionalProperties: false,
10
+ properties: {
11
+ $gt: definition,
12
+ $gte: definition,
13
+ $lt: definition,
14
+ $lte: definition,
15
+ $ne: definition,
16
+ $in: {
17
+ type: 'array',
18
+ items: definition
19
+ },
20
+ $nin: {
21
+ type: 'array',
22
+ items: definition
23
+ }
24
+ }
25
+ }
26
+ ]
27
+ });
28
+ exports.queryProperty = queryProperty;
29
+ const queryArray = (fields) => ({
30
+ type: 'array',
31
+ items: {
32
+ type: 'string',
33
+ enum: fields
34
+ }
35
+ });
36
+ exports.queryArray = queryArray;
37
+ //# sourceMappingURL=query.js.map
@@ -0,0 +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;AAEL,MAAM,UAAU,GAAG,CAA+B,MAAS,EAAE,EAAE,CAAC,CAAC;IACtE,IAAI,EAAE,OAAO;IACb,KAAK,EAAE;QACL,IAAI,EAAE,QAAQ;QACd,IAAI,EAAE,MAAM;KACb;CACQ,CAAA,CAAC;AANC,QAAA,UAAU,cAMX"}
@@ -0,0 +1,24 @@
1
+ import { Schema } from './schema';
2
+ export declare type PropertyResolver<T, V, C> = (value: V | undefined, obj: any, context: C, status: ResolverStatus<T, C>) => Promise<V | undefined>;
3
+ export declare type PropertyResolverMap<T, C> = {
4
+ [key in keyof T]?: PropertyResolver<T, T[key], C>;
5
+ };
6
+ export interface ResolverConfig<T, C> {
7
+ schema?: Schema<any>;
8
+ validate?: 'before' | 'after' | false;
9
+ properties: PropertyResolverMap<T, C>;
10
+ }
11
+ export interface ResolverStatus<T, C> {
12
+ path: string[];
13
+ originalContext?: C;
14
+ properties?: (keyof T)[];
15
+ stack: PropertyResolver<T, any, C>[];
16
+ }
17
+ export declare class Resolver<T, C> {
18
+ options: ResolverConfig<T, C>;
19
+ readonly _type: T;
20
+ constructor(options: ResolverConfig<T, C>);
21
+ resolveProperty<D, K extends keyof T>(name: K, data: D, context: C, status?: Partial<ResolverStatus<T, C>>): Promise<T[K]>;
22
+ resolve<D>(_data: D, context: C, status?: Partial<ResolverStatus<T, C>>): Promise<T>;
23
+ }
24
+ export declare function resolve<T, C>(options: ResolverConfig<T, C>): Resolver<T, C>;
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolve = exports.Resolver = void 0;
4
+ const errors_1 = require("@feathersjs/errors");
5
+ class Resolver {
6
+ constructor(options) {
7
+ this.options = options;
8
+ }
9
+ async resolveProperty(name, data, context, status = {}) {
10
+ const resolver = this.options.properties[name];
11
+ const value = data[name];
12
+ const { path = [], stack = [] } = status || {};
13
+ // This prevents circular dependencies
14
+ if (stack.includes(resolver)) {
15
+ return undefined;
16
+ }
17
+ const resolverStatus = {
18
+ ...status,
19
+ path: [...path, name],
20
+ stack: [...stack, resolver]
21
+ };
22
+ return resolver(value, data, context, resolverStatus);
23
+ }
24
+ async resolve(_data, context, status) {
25
+ const { properties: resolvers, schema, validate } = this.options;
26
+ const data = schema && validate === 'before' ? await schema.validate(_data) : _data;
27
+ const propertyList = (Array.isArray(status === null || status === void 0 ? void 0 : status.properties)
28
+ ? status === null || status === void 0 ? void 0 : status.properties
29
+ // By default get all data and resolver keys but remove duplicates
30
+ : [...new Set(Object.keys(data).concat(Object.keys(resolvers)))]);
31
+ const result = {};
32
+ const errors = {};
33
+ let hasErrors = false;
34
+ // Not the most elegant but better performance
35
+ await Promise.all(propertyList.map(async (name) => {
36
+ const value = data[name];
37
+ if (resolvers[name]) {
38
+ try {
39
+ const resolved = await this.resolveProperty(name, data, context, status);
40
+ if (resolved !== undefined) {
41
+ result[name] = resolved;
42
+ }
43
+ }
44
+ catch (error) {
45
+ // TODO add error stacks
46
+ const convertedError = typeof error.toJSON === 'function'
47
+ ? error.toJSON()
48
+ : { message: error.message || error };
49
+ errors[name] = convertedError;
50
+ hasErrors = true;
51
+ }
52
+ }
53
+ else if (value !== undefined) {
54
+ result[name] = value;
55
+ }
56
+ }));
57
+ if (hasErrors) {
58
+ throw new errors_1.BadRequest(`Error resolving data ${status === null || status === void 0 ? void 0 : status.properties.join('.')}`, errors);
59
+ }
60
+ return schema && validate === 'after'
61
+ ? await schema.validate(result)
62
+ : result;
63
+ }
64
+ }
65
+ exports.Resolver = Resolver;
66
+ function resolve(options) {
67
+ return new Resolver(options);
68
+ }
69
+ exports.resolve = resolve;
70
+ //# sourceMappingURL=resolver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolver.js","sourceRoot":"","sources":["../src/resolver.ts"],"names":[],"mappings":";;;AAAA,+CAAgD;AA2BhD,MAAa,QAAQ;IAGnB,YAAoB,OAA6B;QAA7B,YAAO,GAAP,OAAO,CAAsB;IACjD,CAAC;IAED,KAAK,CAAC,eAAe,CACnB,IAAO,EACP,IAAO,EACP,OAAU,EACV,SAAwC,EAAE;QAE1C,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QAC/C,MAAM,KAAK,GAAI,IAAY,CAAC,IAAI,CAAC,CAAC;QAClC,MAAM,EAAE,IAAI,GAAG,EAAE,EAAE,KAAK,GAAG,EAAE,EAAE,GAAG,MAAM,IAAI,EAAE,CAAC;QAE/C,sCAAsC;QACtC,IAAI,KAAK,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE;YAC5B,OAAO,SAAS,CAAC;SAClB;QAED,MAAM,cAAc,GAAG;YACrB,GAAG,MAAM;YACT,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,IAAc,CAAC;YAC/B,KAAK,EAAE,CAAC,GAAG,KAAK,EAAE,QAAQ,CAAC;SAC5B,CAAA;QAED,OAAO,QAAQ,CAAC,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,cAAc,CAAC,CAAC;IACxD,CAAC;IAED,KAAK,CAAC,OAAO,CAAK,KAAQ,EAAE,OAAU,EAAE,MAAsC;QAC5E,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC;QACjE,MAAM,IAAI,GAAG,MAAM,IAAI,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CA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K,KAAK,SAAS,EAAE;gBAC9B,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;aACtB;QACH,CAAC,CAAC,CAAC,CAAC;QAEJ,IAAI,SAAS,EAAE;YACb,MAAM,IAAI,mBAAU,CAAC,wBAAwB,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;SACtF;QAED,OAAO,MAAM,IAAI,QAAQ,KAAK,OAAO;YACnC,CAAC,CAAC,MAAM,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC;YAC/B,CAAC,CAAC,MAAM,CAAC;IACb,CAAC;CACF;AA5ED,4BA4EC;AAED,SAAgB,OAAO,CAAS,OAA6B;IAC3D,OAAO,IAAI,QAAQ,CAAO,OAAO,CAAC,CAAC;AACrC,CAAC;AAFD,0BAEC"}
@@ -0,0 +1,18 @@
1
+ import Ajv, { AsyncValidateFunction } from 'ajv';
2
+ import { JSONSchema6 } from 'json-schema';
3
+ import { FromSchema, JSONSchema } from 'json-schema-to-ts';
4
+ export declare const AJV: Ajv;
5
+ export declare type JSONSchemaDefinition = JSONSchema & {
6
+ $id: string;
7
+ };
8
+ export declare class Schema<S extends JSONSchemaDefinition> {
9
+ ajv: Ajv;
10
+ validate: AsyncValidateFunction<FromSchema<S>>;
11
+ definition: JSONSchema6;
12
+ propertyNames: string[];
13
+ readonly _type: FromSchema<S>;
14
+ constructor(definition: S, ajv?: Ajv);
15
+ extend<D extends JSONSchemaDefinition>(definition: D): Schema<D & S>;
16
+ toJSON(): JSONSchema6;
17
+ }
18
+ export declare function schema<S extends JSONSchemaDefinition>(definition: S): Schema<S>;
package/lib/schema.js ADDED
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.schema = exports.Schema = exports.AJV = void 0;
7
+ const ajv_1 = __importDefault(require("ajv"));
8
+ exports.AJV = new ajv_1.default({
9
+ coerceTypes: true
10
+ });
11
+ class Schema {
12
+ constructor(definition, ajv = exports.AJV) {
13
+ this.ajv = ajv;
14
+ this.definition = definition;
15
+ this.propertyNames = Object.keys(this.definition.properties);
16
+ this.validate = this.ajv.compile({
17
+ $async: true,
18
+ ...this.definition
19
+ });
20
+ }
21
+ extend(definition) {
22
+ const def = definition;
23
+ const extended = {
24
+ ...this.definition,
25
+ ...def,
26
+ properties: {
27
+ ...this.definition.properties,
28
+ ...def.properties
29
+ }
30
+ };
31
+ return new Schema(extended, this.ajv);
32
+ }
33
+ toJSON() {
34
+ return this.definition;
35
+ }
36
+ }
37
+ exports.Schema = Schema;
38
+ function schema(definition) {
39
+ return new Schema(definition);
40
+ }
41
+ exports.schema = schema;
42
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":";;;;;;AAAA,8CAAiD;AAIpC,QAAA,GAAG,GAAG,IAAI,aAAG,CAAC;IACzB,WAAW,EAAE,IAAI;CAClB,CAAC,CAAC;AAIH,MAAa,MAAM;IAOjB,YAAa,UAAa,EAAE,MAAW,WAAG;QACxC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,UAAU,GAAG,UAAyB,CAAC;QAC5C,IAAI,CAAC,aAAa,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,CAAA;QAC5D,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC;YAC/B,MAAM,EAAE,IAAI;YACZ,GAAG,IAAI,CAAC,UAAU;SACnB,CAAC,CAAC;IACL,CAAC;IAED,MAAM,CAAmC,UAAa;QACpD,MAAM,GAAG,GAAG,UAAyB,CAAC;QACtC,MAAM,QAAQ,GAAG;YACf,GAAG,IAAI,CAAC,UAAU;YAClB,GAAG,GAAG;YACN,UAAU,EAAE;gBACV,GAAG,IAAI,CAAC,UAAU,CAAC,UAAU;gBAC7B,GAAG,GAAG,CAAC,UAAU;aAClB;SACO,CAAC;QAEX,OAAO,IAAI,MAAM,CAAU,QAAe,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;IACxD,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;CACF;AAlCD,wBAkCC;AAED,SAAgB,MAAM,CAAmC,UAAa;IACpE,OAAO,IAAI,MAAM,CAAC,UAAU,CAAC,CAAC;AAChC,CAAC;AAFD,wBAEC"}
package/package.json CHANGED
@@ -1,39 +1,47 @@
1
1
  {
2
2
  "name": "@feathersjs/schema",
3
- "version": "0.0.1-alpha.4",
4
- "description": "Core Feathers schema",
3
+ "description": "A common data schema definition format",
4
+ "version": "5.0.0-pre.15",
5
5
  "homepage": "https://feathersjs.com",
6
+ "main": "lib/",
6
7
  "keywords": [
7
- "feathers"
8
+ "feathers",
9
+ "feathers-plugin"
8
10
  ],
9
11
  "license": "MIT",
10
12
  "funding": {
11
- "type": "opencollective",
12
- "url": "https://opencollective.com/feathers"
13
+ "type": "github",
14
+ "url": "https://github.com/sponsors/daffl"
13
15
  },
14
16
  "repository": {
15
17
  "type": "git",
16
- "url": "git://github.com/feathersjs/schema.git"
18
+ "url": "git://github.com/feathersjs/feathers.git"
17
19
  },
18
20
  "author": {
19
- "name": "Feathers contributor",
21
+ "name": "Feathers contributors",
20
22
  "email": "hello@feathersjs.com",
21
23
  "url": "https://feathersjs.com"
22
24
  },
23
25
  "contributors": [],
24
26
  "bugs": {
25
- "url": "https://github.com/feathersjs/schema/issues"
27
+ "url": "https://github.com/feathersjs/feathers/issues"
26
28
  },
27
29
  "engines": {
28
30
  "node": ">= 12"
29
31
  },
30
- "main": "lib/",
31
- "types": "lib/",
32
+ "files": [
33
+ "CHANGELOG.md",
34
+ "LICENSE",
35
+ "README.md",
36
+ "src/**",
37
+ "lib/**",
38
+ "*.d.ts",
39
+ "*.js"
40
+ ],
32
41
  "scripts": {
33
42
  "prepublish": "npm run compile",
34
43
  "compile": "shx rm -rf lib/ && tsc",
35
- "test": "npm run compile && npm run mocha",
36
- "mocha": "mocha --opts ../../mocha.opts --recursive test/**.test.ts test/**/*.test.ts"
44
+ "test": "mocha --config ../../.mocharc.json --recursive test/**.test.ts test/**/*.test.ts"
37
45
  },
38
46
  "directories": {
39
47
  "lib": "lib"
@@ -42,17 +50,20 @@
42
50
  "access": "public"
43
51
  },
44
52
  "dependencies": {
45
- "@hapi/joi": "^17.0.2",
46
- "@types/hapi__joi": "^16.0.3",
47
- "reflect-metadata": "^0.1.13"
53
+ "@feathersjs/errors": "^5.0.0-pre.15",
54
+ "@feathersjs/feathers": "^5.0.0-pre.15",
55
+ "@types/json-schema": "^7.0.9",
56
+ "ajv": "^8.6.3",
57
+ "json-schema": "^0.3.0",
58
+ "json-schema-to-ts": "^1.6.4"
48
59
  },
49
60
  "devDependencies": {
50
- "@types/mocha": "^5.2.7",
51
- "@types/node": "^13.1.6",
52
- "mocha": "^7.0.1",
53
- "shx": "^0.3.2",
54
- "ts-node": "^8.4.1",
55
- "typescript": "^3.6.4"
56
- },
57
- "gitHead": "b89b2115449d82b94a5c54017330d6ba17f2f8f8"
61
+ "@feathersjs/memory": "^5.0.0-pre.15",
62
+ "@types/mocha": "^9.0.0",
63
+ "@types/node": "^16.11.6",
64
+ "mocha": "^9.1.3",
65
+ "shx": "^0.3.3",
66
+ "typescript": "^4.4.4"
67
+ },
68
+ "gitHead": "8008bf4f8529a2a40b6a2f976c1f43ae13675693"
58
69
  }