@feathersjs/schema
Advanced tools
Comparing version 5.0.0-pre.19 to 5.0.0-pre.20
@@ -6,2 +6,18 @@ # Change Log | ||
# [5.0.0-pre.20](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.19...v5.0.0-pre.20) (2022-05-04) | ||
### Bug Fixes | ||
* **dependencies:** Lock monorepo package version numbers ([#2623](https://github.com/feathersjs/feathers/issues/2623)) ([5640c10](https://github.com/feathersjs/feathers/commit/5640c1020cc139994e695d658c08bad3494db507)) | ||
### Features | ||
* **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)) | ||
# [5.0.0-pre.19](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.18...v5.0.0-pre.19) (2022-05-01) | ||
@@ -8,0 +24,0 @@ |
@@ -0,1 +1,26 @@ | ||
import { JSONSchema } from 'json-schema-to-ts'; | ||
export declare type PropertyQuery<D extends JSONSchema> = { | ||
anyOf: [ | ||
D, | ||
{ | ||
type: 'object'; | ||
additionalProperties: false; | ||
properties: { | ||
$gt: D; | ||
$gte: D; | ||
$lt: D; | ||
$lte: D; | ||
$ne: D; | ||
$in: { | ||
type: 'array'; | ||
items: D; | ||
}; | ||
$nin: { | ||
type: 'array'; | ||
items: D; | ||
}; | ||
}; | ||
} | ||
]; | ||
}; | ||
export declare const queryProperty: <T extends import("json-schema-to-ts").JSONSchema7>(definition: T) => { | ||
@@ -22,1 +47,30 @@ readonly anyOf: readonly [T, { | ||
}; | ||
export declare const queryProperties: <T extends { | ||
[key: string]: import("json-schema-to-ts").JSONSchema7; | ||
}>(definition: T) => { [K in keyof T]: PropertyQuery<T[K]>; }; | ||
export declare const querySyntax: <T extends { | ||
[key: string]: import("json-schema-to-ts").JSONSchema7; | ||
}>(definition: T) => { | ||
readonly $limit: { | ||
readonly type: "number"; | ||
readonly minimum: 0; | ||
}; | ||
readonly $skip: { | ||
readonly type: "number"; | ||
readonly minimum: 0; | ||
}; | ||
readonly $sort: { | ||
readonly type: "object"; | ||
readonly properties: { [K in keyof T]: { | ||
readonly type: 'number'; | ||
readonly enum: [1, -1]; | ||
}; }; | ||
}; | ||
readonly $select: { | ||
readonly type: "array"; | ||
readonly items: { | ||
readonly type: "string"; | ||
readonly enum: (keyof T)[]; | ||
}; | ||
}; | ||
} & { [K_1 in keyof T]: PropertyQuery<T[K_1]>; }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.queryProperty = void 0; | ||
exports.querySyntax = exports.queryProperties = exports.queryProperty = void 0; | ||
const queryProperty = (definition) => ({ | ||
@@ -29,2 +29,36 @@ anyOf: [ | ||
exports.queryProperty = queryProperty; | ||
const queryProperties = (definition) => Object.keys(definition).reduce((res, key) => { | ||
res[key] = (0, exports.queryProperty)(definition[key]); | ||
return res; | ||
}, {}); | ||
exports.queryProperties = queryProperties; | ||
const querySyntax = (definition) => ({ | ||
$limit: { | ||
type: 'number', | ||
minimum: 0 | ||
}, | ||
$skip: { | ||
type: 'number', | ||
minimum: 0 | ||
}, | ||
$sort: { | ||
type: 'object', | ||
properties: Object.keys(definition).reduce((res, key) => { | ||
res[key] = { | ||
type: 'number', | ||
enum: [1, -1] | ||
}; | ||
return res; | ||
}, {}) | ||
}, | ||
$select: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
enum: Object.keys(definition) | ||
} | ||
}, | ||
...(0, exports.queryProperties)(definition) | ||
}); | ||
exports.querySyntax = querySyntax; | ||
//# sourceMappingURL=query.js.map |
@@ -8,2 +8,6 @@ import Ajv, { AsyncValidateFunction, ValidateFunction } from 'ajv'; | ||
$async?: boolean; | ||
properties?: { | ||
[key: string]: JSONSchema; | ||
}; | ||
required?: readonly string[]; | ||
}; | ||
@@ -19,2 +23,4 @@ export interface Schema<T> { | ||
constructor(definition: S, ajv?: Ajv); | ||
get properties(): S["properties"]; | ||
get required(): S["required"]; | ||
validate<T = FromSchema<S>>(...args: Parameters<ValidateFunction<T>>): Promise<T>; | ||
@@ -21,0 +27,0 @@ toJSON(): S; |
@@ -22,2 +22,8 @@ "use strict"; | ||
} | ||
get properties() { | ||
return this.definition.properties; | ||
} | ||
get required() { | ||
return this.definition.required; | ||
} | ||
async validate(...args) { | ||
@@ -24,0 +30,0 @@ try { |
{ | ||
"name": "@feathersjs/schema", | ||
"description": "A common data schema definition format", | ||
"version": "5.0.0-pre.19", | ||
"version": "5.0.0-pre.20", | ||
"homepage": "https://feathersjs.com", | ||
@@ -56,4 +56,4 @@ "main": "lib/", | ||
"dependencies": { | ||
"@feathersjs/errors": "^5.0.0-pre.19", | ||
"@feathersjs/feathers": "^5.0.0-pre.19", | ||
"@feathersjs/errors": "^5.0.0-pre.20", | ||
"@feathersjs/feathers": "^5.0.0-pre.20", | ||
"@types/json-schema": "^7.0.11", | ||
@@ -65,3 +65,3 @@ "ajv": "^8.11.0", | ||
"devDependencies": { | ||
"@feathersjs/memory": "^5.0.0-pre.19", | ||
"@feathersjs/memory": "^5.0.0-pre.20", | ||
"@types/mocha": "^9.1.1", | ||
@@ -74,3 +74,3 @@ "@types/node": "^17.0.31", | ||
}, | ||
"gitHead": "57f3e18bb62735e1869ffafee90286498738fdfa" | ||
"gitHead": "54de749a0b392c7da726c668002b50cafaca530c" | ||
} |
import { JSONSchema } from 'json-schema-to-ts'; | ||
export type PropertyQuery<D extends JSONSchema> = { | ||
anyOf: [ | ||
D, | ||
{ | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
$gt: D, | ||
$gte: D, | ||
$lt: D, | ||
$lte: D, | ||
$ne: D, | ||
$in: { | ||
type: 'array', | ||
items: D | ||
}, | ||
$nin: { | ||
type: 'array', | ||
items: D | ||
} | ||
} | ||
} | ||
] | ||
} | ||
export const queryProperty = <T extends JSONSchema> (definition: T) => ({ | ||
@@ -27,1 +52,38 @@ anyOf: [ | ||
} as const); | ||
export const queryProperties = <T extends { [key: string]: JSONSchema }> (definition: T) => | ||
Object.keys(definition).reduce((res, key) => { | ||
(res as any)[key] = queryProperty(definition[key]) | ||
return res | ||
}, {} as { [K in keyof T]: PropertyQuery<T[K]> }) | ||
export const querySyntax = <T extends { [key: string]: JSONSchema }> (definition: T) => ({ | ||
$limit: { | ||
type: 'number', | ||
minimum: 0 | ||
}, | ||
$skip: { | ||
type: 'number', | ||
minimum: 0 | ||
}, | ||
$sort: { | ||
type: 'object', | ||
properties: Object.keys(definition).reduce((res, key) => { | ||
(res as any)[key] = { | ||
type: 'number', | ||
enum: [1, -1] | ||
} | ||
return res | ||
}, {} as { [K in keyof T]: { readonly type: 'number', readonly enum: [1, -1] } }) | ||
}, | ||
$select: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
enum: Object.keys(definition) as any as (keyof T)[] | ||
} | ||
}, | ||
...queryProperties(definition) | ||
} as const) |
@@ -11,3 +11,8 @@ import Ajv, { AsyncValidateFunction, ValidateFunction } from 'ajv'; | ||
export type JSONSchemaDefinition = JSONSchema & { $id: string, $async?: boolean }; | ||
export type JSONSchemaDefinition = JSONSchema & { | ||
$id: string, | ||
$async?: boolean, | ||
properties?: { [key: string]: JSONSchema } | ||
required?: readonly string[] | ||
}; | ||
@@ -31,2 +36,10 @@ export interface Schema<T> { | ||
get properties () { | ||
return this.definition.properties as S['properties']; | ||
} | ||
get required () { | ||
return this.definition.required as S['required']; | ||
} | ||
async validate <T = FromSchema<S>> (...args: Parameters<ValidateFunction<T>>) { | ||
@@ -33,0 +46,0 @@ try { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45600
812