Files
template-web-astro/src/libs/Schema/Items/SchemaNumber.ts
Florian Bouillon bc97d9106b
Some checks failed
Build, check & Test / run (push) Failing after 1m45s
Lint / run (push) Failing after 48s
Build Docker Image / build_docker (push) Failing after 3m18s
feat: Filemagedon
Signed-off-by: Avior <git@avior.me>
2024-09-11 14:38:58 +02:00

90 lines
1.9 KiB
TypeScript

import SchemaItem from '../SchemaItem'
export default class SchemaNumber extends SchemaItem<number> {
public min(...params: Parameters<SchemaNumber['gte']>): this {
return this.gte(...params)
}
public max(...params: Parameters<SchemaNumber['lte']>): this {
return this.lte(...params)
}
/**
* validate that the number is less or equal than {@link value}
* @param value the maxumum value (inclusive)
* @param message the message sent if not valid
*/
public lte(value: number, message?: string): this {
this.validations.push({
fn(input) {
return input <= value
},
message: message
})
return this
}
/**
* validate that the number is more or equal than {@link value}
* @param value the minimum value (inclusive)
* @param message the message sent if not valid
*/
public gte(value: number, message?: string): this {
this.validations.push({
fn(input) {
return input >= value
},
message: message
})
return this
}
/**
* validate that the number is less than {@link value}
* @param value the maxumum value (exclusive)
* @param message the message sent if not valid
*/
public lt(value: number, message?: string): this {
this.validations.push({
fn(input) {
return input < value
},
message: message
})
return this
}
/**
* validate that the number is more than {@link value}
* @param value the minimum value (exclusive)
* @param message the message sent if not valid
*/
public gt(value: number, message?: string): this {
this.validations.push({
fn(input) {
return input > value
},
message: message
})
return this
}
/**
* Try to parse strings before validating
*/
public parseString(): this {
this.parseActions.push((input) =>
typeof input === 'string' ? Number.parseFloat(input) : input
)
return this
}
public override isOfType(input: unknown): input is number {
return typeof input === 'number' && !Number.isNaN(input)
}
}