@feathersjs/schema
Advanced tools
Comparing version 5.0.0-pre.17 to 5.0.0-pre.18
@@ -6,2 +6,18 @@ # Change Log | ||
# [5.0.0-pre.18](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.17...v5.0.0-pre.18) (2022-04-11) | ||
### Bug Fixes | ||
* **schema:** result resolver correctly resolves paginated find result ([#2594](https://github.com/feathersjs/feathers/issues/2594)) ([6511e45](https://github.com/feathersjs/feathers/commit/6511e45bd0624f1a629530719709f4b27fecbe0b)) | ||
### Features | ||
* **configuration:** Allow app configuration to be validated against a schema ([#2590](https://github.com/feathersjs/feathers/issues/2590)) ([a268f86](https://github.com/feathersjs/feathers/commit/a268f86da92a8ada14ed11ab456aac0a4bba5bb0)) | ||
# [5.0.0-pre.17](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.16...v5.0.0-pre.17) (2022-02-15) | ||
@@ -8,0 +24,0 @@ |
@@ -65,10 +65,12 @@ "use strict"; | ||
const status = context.params.resolve; | ||
const data = context.method === 'find' && context.result.data | ||
? context.result.data | ||
: context.result; | ||
if (Array.isArray(data)) { | ||
context.result = await Promise.all(data.map(current => resolver.resolve(current, ctx, status))); | ||
const isPaginated = context.method === 'find' && context.result.data; | ||
const data = isPaginated ? context.result.data : context.result; | ||
const result = Array.isArray(data) ? | ||
await Promise.all(data.map(async (current) => resolver.resolve(current, ctx, status))) : | ||
await resolver.resolve(data, ctx, status); | ||
if (isPaginated) { | ||
context.result.data = result; | ||
} | ||
else { | ||
context.result = await resolver.resolve(data, ctx, status); | ||
context.result = result; | ||
} | ||
@@ -75,0 +77,0 @@ }; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -6,0 +10,0 @@ if (k2 === undefined) k2 = k; |
@@ -0,1 +1,2 @@ | ||
import { Schema } from './schema'; | ||
export declare type PropertyResolver<T, V, C> = (value: V | undefined, obj: T, context: C, status: ResolverStatus<T, C>) => Promise<V | undefined>; | ||
@@ -6,3 +7,3 @@ export declare type PropertyResolverMap<T, C> = { | ||
export interface ResolverConfig<T, C> { | ||
schema?: any; | ||
schema?: Schema<T>; | ||
validate?: 'before' | 'after' | false; | ||
@@ -9,0 +10,0 @@ properties: PropertyResolverMap<T, C>; |
import Ajv, { AsyncValidateFunction, ValidateFunction } from 'ajv'; | ||
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; | ||
export declare const AJV: Ajv; | ||
export declare const DEFAULT_AJV: Ajv; | ||
export { Ajv }; | ||
export declare type JSONSchemaDefinition = JSONSchema & { | ||
@@ -8,3 +9,6 @@ $id: string; | ||
}; | ||
export declare class Schema<S extends JSONSchemaDefinition> { | ||
export interface Schema<T> { | ||
validate<X = T>(...args: Parameters<ValidateFunction<X>>): Promise<X>; | ||
} | ||
export declare class SchemaWrapper<S extends JSONSchemaDefinition> implements Schema<FromSchema<S>> { | ||
definition: S; | ||
@@ -18,2 +22,2 @@ ajv: Ajv; | ||
} | ||
export declare function schema<S extends JSONSchemaDefinition>(definition: S, ajv?: Ajv): Schema<S>; | ||
export declare function schema<S extends JSONSchemaDefinition>(definition: S, ajv?: Ajv): SchemaWrapper<S>; |
@@ -6,10 +6,11 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.schema = exports.Schema = exports.AJV = void 0; | ||
exports.schema = exports.SchemaWrapper = exports.Ajv = exports.DEFAULT_AJV = void 0; | ||
const ajv_1 = __importDefault(require("ajv")); | ||
exports.Ajv = ajv_1.default; | ||
const errors_1 = require("@feathersjs/errors"); | ||
exports.AJV = new ajv_1.default({ | ||
exports.DEFAULT_AJV = new ajv_1.default({ | ||
coerceTypes: true | ||
}); | ||
class Schema { | ||
constructor(definition, ajv = exports.AJV) { | ||
class SchemaWrapper { | ||
constructor(definition, ajv = exports.DEFAULT_AJV) { | ||
this.definition = definition; | ||
@@ -35,7 +36,7 @@ this.ajv = ajv; | ||
} | ||
exports.Schema = Schema; | ||
function schema(definition, ajv = exports.AJV) { | ||
return new Schema(definition, ajv); | ||
exports.SchemaWrapper = SchemaWrapper; | ||
function schema(definition, ajv = exports.DEFAULT_AJV) { | ||
return new SchemaWrapper(definition, ajv); | ||
} | ||
exports.schema = schema; | ||
//# sourceMappingURL=schema.js.map |
{ | ||
"name": "@feathersjs/schema", | ||
"description": "A common data schema definition format", | ||
"version": "5.0.0-pre.17", | ||
"version": "5.0.0-pre.18", | ||
"homepage": "https://feathersjs.com", | ||
"main": "lib/", | ||
"types": "lib/", | ||
"keywords": [ | ||
@@ -18,3 +19,4 @@ "feathers", | ||
"type": "git", | ||
"url": "git://github.com/feathersjs/feathers.git" | ||
"url": "git://github.com/feathersjs/feathers.git", | ||
"directory": "packages/schema" | ||
}, | ||
@@ -45,3 +47,4 @@ "author": { | ||
"compile": "shx rm -rf lib/ && tsc", | ||
"test": "mocha --config ../../.mocharc.json --recursive test/**.test.ts test/**/*.test.ts" | ||
"mocha": "mocha --config ../../.mocharc.json --recursive test/**.test.ts test/**/*.test.ts", | ||
"test": "npm run compile && npm run mocha" | ||
}, | ||
@@ -55,6 +58,6 @@ "directories": { | ||
"dependencies": { | ||
"@feathersjs/errors": "^5.0.0-pre.17", | ||
"@feathersjs/feathers": "^5.0.0-pre.17", | ||
"@types/json-schema": "^7.0.9", | ||
"ajv": "^8.10.0", | ||
"@feathersjs/errors": "^5.0.0-pre.18", | ||
"@feathersjs/feathers": "^5.0.0-pre.18", | ||
"@types/json-schema": "^7.0.11", | ||
"ajv": "^8.11.0", | ||
"json-schema": "^0.4.0", | ||
@@ -64,11 +67,11 @@ "json-schema-to-ts": "^1.6.5" | ||
"devDependencies": { | ||
"@feathersjs/memory": "^5.0.0-pre.17", | ||
"@feathersjs/memory": "^5.0.0-pre.18", | ||
"@types/mocha": "^9.1.0", | ||
"@types/node": "^17.0.15", | ||
"@types/node": "^17.0.23", | ||
"ajv-formats": "^2.1.1", | ||
"mocha": "^9.2.0", | ||
"mocha": "^9.2.2", | ||
"shx": "^0.3.4", | ||
"typescript": "^4.5.5" | ||
"typescript": "^4.6.3" | ||
}, | ||
"gitHead": "d828748e57b40abfaa15710663afed417de14a1d" | ||
"gitHead": "c0b7b67d872dcd6b6d94e4587f21332c8a519b50" | ||
} |
@@ -5,2 +5,3 @@ # @feathersjs/schema | ||
[![Download Status](https://img.shields.io/npm/dm/@feathersjs/schema.svg?style=flat-square)](https://www.npmjs.com/package/@feathersjs/schema) | ||
[![Discord](https://badgen.net/badge/icon/discord?icon=discord&label)](https://discord.gg/qa8kez8QBx) | ||
@@ -7,0 +8,0 @@ > A common data schema definition format |
@@ -76,12 +76,14 @@ import { HookContext, NextFunction } from '@feathersjs/feathers'; | ||
const status = context.params.resolve; | ||
const data = context.method === 'find' && context.result.data | ||
? context.result.data | ||
: context.result; | ||
if (Array.isArray(data)) { | ||
context.result = await Promise.all(data.map(current => | ||
resolver.resolve(current, ctx, status) | ||
)); | ||
const isPaginated = context.method === 'find' && context.result.data; | ||
const data = isPaginated ? context.result.data : context.result; | ||
const result = Array.isArray(data) ? | ||
await Promise.all(data.map(async current => resolver.resolve(current, ctx, status))) : | ||
await resolver.resolve(data, ctx, status); | ||
if (isPaginated) { | ||
context.result.data = result; | ||
} else { | ||
context.result = await resolver.resolve(data, ctx, status); | ||
context.result = result; | ||
} | ||
@@ -88,0 +90,0 @@ }; |
import { BadRequest } from '@feathersjs/errors'; | ||
import { Schema } from './schema'; | ||
@@ -15,5 +16,3 @@ export type PropertyResolver<T, V, C> = ( | ||
export interface ResolverConfig<T, C> { | ||
// TODO this should be `Schema<any>` but has recently produced an error, see | ||
// https://github.com/ThomasAribart/json-schema-to-ts/issues/53 | ||
schema?: any, | ||
schema?: Schema<T>, | ||
validate?: 'before'|'after'|false, | ||
@@ -75,3 +74,3 @@ properties: PropertyResolverMap<T, C> | ||
await Promise.all(propertyList.map(async name => { | ||
const value = data[name]; | ||
const value = (data as any)[name]; | ||
@@ -78,0 +77,0 @@ if (resolvers[name]) { |
@@ -5,9 +5,15 @@ import Ajv, { AsyncValidateFunction, ValidateFunction } from 'ajv'; | ||
export const AJV = new Ajv({ | ||
export const DEFAULT_AJV = new Ajv({ | ||
coerceTypes: true | ||
}); | ||
export { Ajv }; | ||
export type JSONSchemaDefinition = JSONSchema & { $id: string, $async?: boolean }; | ||
export class Schema<S extends JSONSchemaDefinition> { | ||
export interface Schema<T> { | ||
validate <X = T> (...args: Parameters<ValidateFunction<X>>): Promise<X>; | ||
} | ||
export class SchemaWrapper<S extends JSONSchemaDefinition> implements Schema<FromSchema<S>> { | ||
ajv: Ajv; | ||
@@ -17,3 +23,3 @@ validator: AsyncValidateFunction; | ||
constructor (public definition: S, ajv: Ajv = AJV) { | ||
constructor (public definition: S, ajv: Ajv = DEFAULT_AJV) { | ||
this.ajv = ajv; | ||
@@ -41,4 +47,4 @@ this.validator = this.ajv.compile({ | ||
export function schema <S extends JSONSchemaDefinition> (definition: S, ajv: Ajv = AJV) { | ||
return new Schema(definition, ajv); | ||
export function schema <S extends JSONSchemaDefinition> (definition: S, ajv: Ajv = DEFAULT_AJV) { | ||
return new SchemaWrapper(definition, ajv); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
38040
630
24
Updated@types/json-schema@^7.0.11
Updatedajv@^8.11.0