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): boolean
import { 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[]): boolean
import { 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']
): boolean
import { hms } from '@globalbrain/sefirot/lib/validation/validators'
const time = {
hour: '10',
minute: '61', // Invalid value.
second: '00'
}
hms(time) // <- false
You 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']) // <- true
maxFileSize
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): boolean
import { 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): boolean
import { 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): boolean
import { month } from '@globalbrain/sefirot/lib/validation/validators'
month(13) // <- false
requiredHms
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']
): boolean
import { requiredHms } from '@globalbrain/sefirot/lib/validation/validators'
const time = {
hour: '10',
minute: null, // Value missing.
second: '00'
}
requiredHms(time) // <- false
You 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']) // <- true
requiredYmd
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']
): boolean
import { requiredYmd } from '@globalbrain/sefirot/lib/validation/validators'
const date = {
year: 1985,
month: null, // Value missing.
date: 10
}
requiredHms(date) // <- false
You 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']) // <- true
ymd
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']
): boolean
import { ymd } from '@globalbrain/sefirot/lib/validation/validators'
const date = {
year: 1985,
month: 15, // Invalid value.
date: 10
}
ymd(date) // <- false
You 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