@tsed/schema
Version:
JsonSchema module for Ts.ED Framework
144 lines (143 loc) • 2.73 kB
JavaScript
import { withErrorMsg } from "../../utils/withErrorMsg.js";
import { JsonEntityFn } from "./jsonEntityFn.js";
/**
* The value of `minimum` MUST be a number, representing an inclusive upper limit for a numeric instance.
*
* If the instance is a number, then this keyword validates only if the instance is greater than or exactly equal to `minimum`.
*
* ::: warning
* This decorator will be removed in v7.
* For v6 user, use @@Minimum@@ from @tsed/schema instead of @tsed/platform-http.
* :::
*
* ## Example
* ### With primitive type
*
* ```typescript
* class Model {
* @Minimum(10)
* property: number;
* }
* ```
*
* Will produce:
*
* ```json
* {
* "type": "object",
* "properties": {
* "property": {
* "type": "number",
* "minimum": 10
* }
* }
* }
* ```
*
* ### With array type
*
* ```typescript
* class Model {
* @Minimum(10)
* @CollectionOf(Number)
* property: number[];
* }
* ```
*
* Will produce:
*
* ```json
* {
* "type": "object",
* "properties": {
* "property": {
* "type": "array",
* "items": {
* "type": "number",
* "minimum": 10
* }
* }
* }
* }
* ```
*
* @param {number} minimum The minimum value allowed
* @param {boolean} exclusive
* @decorator
* @validation
* @swagger
* @schema
* @input
* @ajv-errors
*/
export const Minimum = withErrorMsg("minimum", (minimum, exclusive = false) => {
return JsonEntityFn((store) => {
exclusive ? store.itemSchema.exclusiveMinimum(minimum) : store.itemSchema.minimum(minimum);
});
});
/**
* The value of `minimum` MUST be a number, representing an inclusive upper limit for a numeric instance.
*
* If the instance is a number, then this keyword validates only if the instance is greater than or exactly equal to `minimum`.
*
* ## Example
* ### With primitive type
*
* ```typescript
* class Model {
* @Min(10)
* property: number;
* }
* ```
*
* Will produce:
*
* ```json
* {
* "type": "object",
* "properties": {
* "property": {
* "type": "number",
* "minimum": 10
* }
* }
* }
* ```
*
* ### With array type
*
* ```typescript
* class Model {
* @Min(10)
* @CollectionOf(Number)
* property: number[];
* }
* ```
*
* Will produce:
*
* ```json
* {
* "type": "object",
* "properties": {
* "property": {
* "type": "array",
* "items": {
* "type": "number",
* "minimum": 10
* }
* }
* }
* }
* ```
*
* @alias Minimum
* @param minimum The minimum value allowed
* @param exclusive
* @decorator
* @validation
* @swagger
* @schema
* @input
*/
export const Min = Minimum;