export type Validator = (value: xValue) => string[] export function validator( ...conditions: Validator[] ): Validator { return value => { let problems: string[] = [] for (const condition of conditions) { problems = condition(value) if (problems.length > 0) break } return problems } } export function multi(...conditions: Validator[]): Validator { return value => { const problems: string[] = [] for (const condition of conditions) { for (const problem of condition(value)) problems.push(problem) } return problems } } export function schema(schematic: { [P in keyof xObject]: Validator }): Validator { const schematicKeys = Object.keys(schematic) return object => ( Object.entries(object).flatMap( ([key, value]) => schematicKeys.includes(key) ? (>schematic[key])(value) .map(problem => `"${key}": ${problem}`) : [`"${key} not in schema"`] ) ) } export function branch(...conditions: Validator[]): Validator { return value => { const results = conditions.map(condition => condition(value)) let anySuccess = false for (const problems of results) if (problems.length === 0) anySuccess = true return anySuccess ? [] : results.flat() .map((problem, index) => index > 0 ? `or, ${problem}` : problem) } } export function depend( first: Validator, ...conditions: Validator[] ): Validator { return value => { const problems = first(value) if (problems.length === 0) { for (const condition of conditions) { for (const problem of condition(value)) problems.push(problem) } } return problems } } export function each( ...conditions: Validator[] ): Validator { return arr => { if (!Array.isArray(arr)) return ["must be array"] const validate = validator(...conditions) const problems: string[] = [] arr.forEach((item, index) => { for (const problem of validate(item)) problems.push(`(${index + 1}) ${problem}`) }) return problems } } export const is = (x: X): Validator => value => value === x ? [] : ["wrong value"] export const defined = (): Validator => value => (value === undefined || value === null) ? ["must be defined (not undefined or null)"] : [] export const notDefined = (): Validator => value => (value !== undefined && value !== null) ? ["must be undefined or null"] : [] export const string = (): Validator => value => typeof value !== "string" ? ["must be a string"] : [] export const number = (): Validator => value => typeof value !== "number" ? ["must be a number"] : [] export const boolean = (): Validator => value => typeof value !== "boolean" ? ["must be a boolean"] : [] export const greaterThan = (threshold: number): Validator => value => value > threshold ? [] : ["too small"] export const min = (threshold: number): Validator => value => value < threshold ? ["too small"] : [] export const max = (threshold: number): Validator => value => value > threshold ? ["too big"] : [] export const array = (): Validator => value => Array.isArray(value) ? [] : ["must be an array"] export const length = (len: number): Validator<{length: number}> => value => value.length !== len ? [`length must be ${len}`] : [] export const minLength = (min: number): Validator<{length: number}> => value => value.length < min ? ["too small"] : [] export const maxLength = (max: number): Validator<{length: number}> => value => value.length > max ? ["too big"] : [] export const notWhitespace = (): Validator => value => value.length > 0 && value.trim().length === 0 ? ["can't be all whitespace"] : [] export const zeroWhitespace = (): Validator => value => /\s/.test(value) ? ["must not have any spaces"] : [] export const url = (): Validator => value => { try { void new URL(value) return [] } catch (error) { return ["invalid url"] } } export const localhost = (): Validator => value => { return /^https?:\/\/(127\.0\.0\.1|localhost)(|:\d{1,5})(|\/\S*)$/i.test(value) ? [] : ["must be a localhost address"] } export const https = (): Validator => value => { return /^https:\/\//i.test(value) ? [] : ["must be secure, starting with 'https'"] } export const origin = (): Validator => value => /^https?:\/\/[a-zA-Z\.\d-]+(?:|:\d+)$/i.test(value) ? [] : ["invalid origin"] export const regex = (r: RegExp, problem: string = "invalid string"): Validator => value => r.test(value) ? [] : [problem] const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ export const email = (): Validator => value => emailRegex.test(value) ? [] : ["invalid email"] // const labelValidator = validator(string(), minLength(1), maxLength(50), notWhitespace()) // const problems = labelValidator("hello") // const problems2 = validator(array(string()))