Validators
The Validators provides set of functions to validate values. Sefirot encourages to use builtin validatoes provided by Vuelidate library. These validators are which is not provided by Vuelidate.
You may import functions from validation/validators.
import { ... } from '@globalbrain/sefirot/lib/validation/validators'checked
Checks if the given value is true. This is useful for validating inputs for things like a checkbox.
function checked(value: boolean): booleanimport { checked } from '@globalbrain/sefirot/lib/validation/validators'
checked(false)fileExtension
Checks if the given File has a given extension.
function fileExtension(file: File, extensions: string[]): booleanimport { fileExtension } from '@globalbrain/sefirot/lib/validation/validators'
fileExtension(file, ['jpg', 'png'])hms
Checks if the given hour, minute and second has a valid value. This validator will not validate the presence of the value so it will return true if a value is null. To check the presence of the value, use requiredHms validator.
import { type Hms } from '@globalbrain/sefirot/lib/support/Day'
function hms(
hms: Hms,
required: ('h' | 'm' | 's')[] = ['h', 'm', 's']
): booleanimport { hms } from '@globalbrain/sefirot/lib/validation/validators'
const time = {
hour: '10',
minute: '61', // Invalid value.
second: '00'
}
hms(time) // <- falseYou may pass 2nd argument to specify which fields to validate. For example,if you pass ['h', 'm'], it will validate only hour and minute.
const time = {
hour: '10',
minute: '30',
second: '61' // Invalid seconds, but this will be ignored.
}
hms(time, ['h', 'm']) // <- truemaxFileSize
Checks if the given File in smaller the given size. You can specify size in b, kb, mb or gb by passing a string such as 100mb or 2gb.
function maxFileSize(file: File, size: string): booleanimport { maxFileSize } from '@globalbrain/sefirot/lib/validation/validators'
maxFileSize(file, '100mb')maxTotalFileSize
Checks if the total size of the given files is smaller the specified size. You can specify size in b, kb, mb or gb by passing a string such as 100mb or 2gb.
function maxTotalFileSize(files: File[], size: string): booleanimport { maxTotalFileSize } from '@globalbrain/sefirot/lib/validation/validators'
maxTotalFileSize([fileA, fileB], '100mb')month
Checks if the given number is a valid month number (1–12).
function month(value: number): booleanimport { month } from '@globalbrain/sefirot/lib/validation/validators'
month(13) // <- falserequiredHms
Checks if the given hour, minute and second is present. This validator will not validate if the values are valid numbers. To check the validity of the value, use hms validator.
import { type Hms } from '@globalbrain/sefirot/lib/support/Day'
function requiredHms(
hms: Hms,
required: ('h' | 'm' | 's')[] = ['h', 'm', 's']
): booleanimport { requiredHms } from '@globalbrain/sefirot/lib/validation/validators'
const time = {
hour: '10',
minute: null, // Value missing.
second: '00'
}
requiredHms(time) // <- falseYou may pass 2nd argument to specify which fields to validate. For example,if you pass ['h', 'm'], it will validate only hour and minute.
const time = {
hour: '10',
minute: '30',
second: null // Valud missing, but this will be ignored.
}
hms(time, ['h', 'm']) // <- truerequiredYmd
Checks if the given year, month and date is present. This validator will not validate if the values are valid numbers. To check the validity of the value, use ymd validator.
import { type Ymd } from '@globalbrain/sefirot/lib/support/Day'
function requiredYmd(
ymd: Ymd,
required: ('y' | 'm' | 'd')[] = ['y', 'm', 'd']
): booleanimport { requiredYmd } from '@globalbrain/sefirot/lib/validation/validators'
const date = {
year: 1985,
month: null, // Value missing.
date: 10
}
requiredHms(date) // <- falseYou may pass 2nd argument to specify which fields to validate. For example,if you pass ['y', 'm'], it will validate only year and month.
const date = {
year: 1985,
month: 10,
date: null // Valud missing, but this will be ignored.
}
hms(date, ['y', 'm']) // <- trueymd
Checks if the given year, month and date has a valid value. This validator will not validate the presence of the value so it will return true if a value is null. To check the presence of the value, use requiredYmd validator.
import { type Ymd } from '@globalbrain/sefirot/lib/support/Day'
function ymd(
ymd: Ymd,
required: ('y' | 'm' | 'd')[] = ['y', 'm', 'd']
): booleanimport { ymd } from '@globalbrain/sefirot/lib/validation/validators'
const date = {
year: 1985,
month: 15, // Invalid value.
date: 10
}
ymd(date) // <- falseYou may pass 2nd argument to specify which fields to validate. For example,if you pass ['y', 'm'], it will validate only year and month.
const date = {
hour: 1985,
minute: 10,
second: 35 // Invalid date, but this will be ignored.
}
ymd(date, ['y', 'm']) // <- true