@vinejs/vine
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -5,3 +5,3 @@ import type { FieldContext, ValidationFields, ValidationMessages, MessagesProviderContact } from '../types.js'; | ||
constructor(messages: ValidationMessages, fields: ValidationFields); | ||
getMessage(rawMessage: string, rule: string, ctx: FieldContext, args?: Record<string, any>): string; | ||
getMessage(rawMessage: string, rule: string, field: FieldContext, args?: Record<string, any>): string; | ||
} |
@@ -25,8 +25,8 @@ export class SimpleMessagesProvider { | ||
} | ||
getMessage(rawMessage, rule, ctx, args) { | ||
const field = this.#fields[ctx.fieldName] || ctx.fieldName; | ||
const fieldMessage = this.#messages[`${ctx.wildCardPath}.${rule}`]; | ||
getMessage(rawMessage, rule, field, args) { | ||
const fieldName = this.#fields[field.name] || field.name; | ||
const fieldMessage = this.#messages[`${field.wildCardPath}.${rule}`]; | ||
if (fieldMessage) { | ||
return this.#interpolate(fieldMessage, { | ||
field, | ||
field: fieldName, | ||
...args, | ||
@@ -38,3 +38,3 @@ }); | ||
return this.#interpolate(ruleMessage, { | ||
field, | ||
field: fieldName, | ||
...args, | ||
@@ -44,3 +44,3 @@ }); | ||
return this.#interpolate(rawMessage, { | ||
field, | ||
field: fieldName, | ||
...args, | ||
@@ -47,0 +47,0 @@ }); |
@@ -13,5 +13,5 @@ import { ValidationError } from '../errors/validation_error.js'; | ||
errors: SimpleError[]; | ||
report(message: string, rule: string, ctx: FieldContext, meta?: Record<string, any> | undefined): void; | ||
report(message: string, rule: string, field: FieldContext, meta?: Record<string, any> | undefined): void; | ||
createError(): ValidationError; | ||
} | ||
export {}; |
@@ -5,7 +5,7 @@ import { E_VALIDATION_ERROR } from '../errors/main.js'; | ||
errors = []; | ||
report(message, rule, ctx, meta) { | ||
report(message, rule, field, meta) { | ||
const error = { | ||
message, | ||
rule, | ||
field: ctx.wildCardPath, | ||
field: field.wildCardPath, | ||
}; | ||
@@ -15,4 +15,4 @@ if (meta) { | ||
} | ||
if (ctx.isArrayMember) { | ||
error.index = ctx.fieldName; | ||
if (field.isArrayMember) { | ||
error.index = field.name; | ||
} | ||
@@ -19,0 +19,0 @@ this.hasErrors = true; |
import { messages } from '../../defaults.js'; | ||
import { createRule } from '../../vine/create_rule.js'; | ||
const ACCEPTED_VALUES = ['on', '1', 'yes', 'true', true, 1]; | ||
export const acceptedRule = createRule((value, _, ctx) => { | ||
export const acceptedRule = createRule((value, _, field) => { | ||
if (!ACCEPTED_VALUES.includes(value)) { | ||
ctx.report(messages.accepted, 'accepted', ctx); | ||
field.report(messages.accepted, 'accepted', field); | ||
} | ||
}); |
import { helpers } from '../../vine/helpers.js'; | ||
import { messages } from '../../defaults.js'; | ||
import { createRule } from '../../vine/create_rule.js'; | ||
export const minLengthRule = createRule((value, options, ctx) => { | ||
if (!ctx.isValid) { | ||
export const minLengthRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (value.length < options.min) { | ||
ctx.report(messages['array.minLength'], 'minLength', ctx, options); | ||
field.report(messages['array.minLength'], 'minLength', field, options); | ||
} | ||
}); | ||
export const maxLengthRule = createRule((value, options, ctx) => { | ||
if (!ctx.isValid) { | ||
export const maxLengthRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (value.length > options.max) { | ||
ctx.report(messages['array.maxLength'], 'maxLength', ctx, options); | ||
field.report(messages['array.maxLength'], 'maxLength', field, options); | ||
} | ||
}); | ||
export const fixedLengthRule = createRule((value, options, ctx) => { | ||
if (!ctx.isValid) { | ||
export const fixedLengthRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (value.length !== options.size) { | ||
ctx.report(messages['array.fixedLength'], 'fixedLength', ctx, options); | ||
field.report(messages['array.fixedLength'], 'fixedLength', field, options); | ||
} | ||
}); | ||
export const notEmptyRule = createRule((value, _, ctx) => { | ||
if (!ctx.isValid) { | ||
export const notEmptyRule = createRule((value, _, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (value.length <= 0) { | ||
ctx.report(messages.notEmpty, 'notEmpty', ctx); | ||
field.report(messages.notEmpty, 'notEmpty', field); | ||
} | ||
}); | ||
export const distinctRule = createRule((value, options, ctx) => { | ||
if (!ctx.isValid) { | ||
export const distinctRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (!helpers.isDistinct(value, options.fields)) { | ||
ctx.report(messages.distinct, 'distinct', ctx, options); | ||
field.report(messages.distinct, 'distinct', field, options); | ||
} | ||
}); | ||
export const compactRule = createRule((value, _, ctx) => { | ||
if (!ctx.isValid) { | ||
export const compactRule = createRule((value, _, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
ctx.mutate(value.filter((item) => helpers.exists(item) && item !== ''), ctx); | ||
field.mutate(value.filter((item) => helpers.exists(item) && item !== ''), field); | ||
}); |
import { helpers } from '../../vine/helpers.js'; | ||
import { messages } from '../../defaults.js'; | ||
import { createRule } from '../../vine/create_rule.js'; | ||
export const booleanRule = createRule((value, options, ctx) => { | ||
export const booleanRule = createRule((value, options, field) => { | ||
const valueAsBoolean = options.strict === true ? value : helpers.asBoolean(value); | ||
if (typeof valueAsBoolean !== 'boolean') { | ||
ctx.report(messages.boolean, 'boolean', ctx); | ||
field.report(messages.boolean, 'boolean', field); | ||
return; | ||
} | ||
ctx.mutate(valueAsBoolean, ctx); | ||
field.mutate(valueAsBoolean, field); | ||
}); |
@@ -34,3 +34,3 @@ import Macroable from '@poppinss/macroable'; | ||
record<Schema extends SchemaTypes>(schema: Schema): VineRecord<Schema>; | ||
enum<const Values extends readonly unknown[]>(values: Values | ((ctx: FieldContext) => Values)): VineEnum<Values>; | ||
enum<const Values extends readonly unknown[]>(values: Values | ((field: FieldContext) => Values)): VineEnum<Values>; | ||
enum<Values extends EnumLike>(values: Values): VineNativeEnum<Values>; | ||
@@ -37,0 +37,0 @@ any(): VineAny; |
@@ -7,9 +7,9 @@ import { BaseLiteralType } from '../base/literal.js'; | ||
enum: (options: { | ||
choices: readonly any[] | ((ctx: FieldContext) => readonly any[]); | ||
choices: readonly any[] | ((field: FieldContext) => readonly any[]); | ||
}) => Validation<{ | ||
choices: readonly any[] | ((ctx: FieldContext) => readonly any[]); | ||
choices: readonly any[] | ((field: FieldContext) => readonly any[]); | ||
}>; | ||
}; | ||
constructor(values: Values | ((ctx: FieldContext) => Values), options?: FieldOptions, validations?: Validation<any>[]); | ||
constructor(values: Values | ((field: FieldContext) => Values), options?: FieldOptions, validations?: Validation<any>[]); | ||
clone(): this; | ||
} |
@@ -7,5 +7,5 @@ import { BaseLiteralType } from '../base/literal.js'; | ||
enum: (options: { | ||
choices: readonly any[] | ((ctx: import("@vinejs/compiler/types").FieldContext) => readonly any[]); | ||
choices: readonly any[] | ((field: import("@vinejs/compiler/types").FieldContext) => readonly any[]); | ||
}) => Validation<{ | ||
choices: readonly any[] | ((ctx: import("@vinejs/compiler/types").FieldContext) => readonly any[]); | ||
choices: readonly any[] | ((field: import("@vinejs/compiler/types").FieldContext) => readonly any[]); | ||
}>; | ||
@@ -12,0 +12,0 @@ }; |
import { FieldContext } from '@vinejs/compiler/types'; | ||
export declare const enumRule: (options: { | ||
choices: readonly any[] | ((ctx: FieldContext) => readonly any[]); | ||
choices: readonly any[] | ((field: FieldContext) => readonly any[]); | ||
}) => import("../../types.js").Validation<{ | ||
choices: readonly any[] | ((ctx: FieldContext) => readonly any[]); | ||
choices: readonly any[] | ((field: FieldContext) => readonly any[]); | ||
}>; |
import { createRule } from '../../vine/create_rule.js'; | ||
import { messages } from '../../defaults.js'; | ||
export const enumRule = createRule((value, options, ctx) => { | ||
const choices = typeof options.choices === 'function' ? options.choices(ctx) : options.choices; | ||
export const enumRule = createRule((value, options, field) => { | ||
const choices = typeof options.choices === 'function' ? options.choices(field) : options.choices; | ||
if (!choices.includes(value)) { | ||
ctx.report(messages.enum, 'enum', ctx, { choices }); | ||
field.report(messages.enum, 'enum', field, { choices }); | ||
} | ||
}); |
@@ -0,5 +1,5 @@ | ||
import { messages } from '../../defaults.js'; | ||
import { helpers } from '../../vine/helpers.js'; | ||
import { messages } from '../../defaults.js'; | ||
import { createRule } from '../../vine/create_rule.js'; | ||
export const equalsRule = createRule((value, options, ctx) => { | ||
export const equalsRule = createRule((value, options, field) => { | ||
let input = value; | ||
@@ -13,6 +13,6 @@ if (typeof options.expectedValue === 'boolean') { | ||
if (input !== options.expectedValue) { | ||
ctx.report(messages.literal, 'literal', ctx, options); | ||
field.report(messages.literal, 'literal', field, options); | ||
return; | ||
} | ||
ctx.mutate(input, ctx); | ||
field.mutate(input, field); | ||
}); |
import { helpers } from '../../vine/helpers.js'; | ||
import { createRule } from '../../vine/create_rule.js'; | ||
import { messages } from '../../defaults.js'; | ||
export const numberRule = createRule((value, _, ctx) => { | ||
export const numberRule = createRule((value, _, field) => { | ||
const valueAsNumber = helpers.asNumber(value); | ||
@@ -9,49 +9,49 @@ if (Number.isNaN(valueAsNumber) || | ||
valueAsNumber === Number.NEGATIVE_INFINITY) { | ||
ctx.report(messages.number, 'number', ctx); | ||
field.report(messages.number, 'number', field); | ||
return; | ||
} | ||
ctx.mutate(valueAsNumber, ctx); | ||
field.mutate(valueAsNumber, field); | ||
}); | ||
export const minRule = createRule((value, options, ctx) => { | ||
if (!ctx.isValid) { | ||
export const minRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (value < options.min) { | ||
ctx.report(messages.min, 'min', ctx, options); | ||
field.report(messages.min, 'min', field, options); | ||
} | ||
}); | ||
export const maxRule = createRule((value, options, ctx) => { | ||
if (!ctx.isValid) { | ||
export const maxRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (value > options.max) { | ||
ctx.report(messages.max, 'max', ctx, options); | ||
field.report(messages.max, 'max', field, options); | ||
} | ||
}); | ||
export const rangeRule = createRule((value, options, ctx) => { | ||
if (!ctx.isValid) { | ||
export const rangeRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (value < options.min || value > options.max) { | ||
ctx.report(messages.range, 'range', ctx, options); | ||
field.report(messages.range, 'range', field, options); | ||
} | ||
}); | ||
export const positiveRule = createRule((value, _, ctx) => { | ||
if (!ctx.isValid) { | ||
export const positiveRule = createRule((value, _, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (value < 0) { | ||
ctx.report(messages.positive, 'positive', ctx); | ||
field.report(messages.positive, 'positive', field); | ||
} | ||
}); | ||
export const negativeRule = createRule((value, _, ctx) => { | ||
if (!ctx.isValid) { | ||
export const negativeRule = createRule((value, _, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (value >= 0) { | ||
ctx.report(messages.negative, 'negative', ctx); | ||
field.report(messages.negative, 'negative', field); | ||
} | ||
}); | ||
export const decimalRule = createRule((value, options, ctx) => { | ||
if (!ctx.isValid) { | ||
export const decimalRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
@@ -63,12 +63,12 @@ } | ||
})) { | ||
ctx.report(messages.decimal, 'decimal', ctx, { digits: options.range.join('-') }); | ||
field.report(messages.decimal, 'decimal', field, { digits: options.range.join('-') }); | ||
} | ||
}); | ||
export const withoutDecimalsRule = createRule((value, _, ctx) => { | ||
if (!ctx.isValid) { | ||
export const withoutDecimalsRule = createRule((value, _, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (!Number.isInteger(value)) { | ||
ctx.report(messages.withoutDecimals, 'withoutDecimals', ctx); | ||
field.report(messages.withoutDecimals, 'withoutDecimals', field); | ||
} | ||
}); |
@@ -8,5 +8,5 @@ import { ObjectGroup } from './group.js'; | ||
export declare namespace group { | ||
var _a: <Properties extends Record<string, SchemaTypes>>(conditon: (value: Record<string, unknown>, ctx: FieldContext) => any, properties: Properties) => GroupConditional<Properties, { [K in keyof Properties]: Properties[K][typeof OTYPE]; }, { [K_1 in keyof Properties as CamelCase<K_1 & string>]: Properties[K_1][typeof COTYPE]; }>; | ||
var _a: <Properties extends Record<string, SchemaTypes>>(conditon: (value: Record<string, unknown>, field: FieldContext) => any, properties: Properties) => GroupConditional<Properties, { [K in keyof Properties]: Properties[K][typeof OTYPE]; }, { [K_1 in keyof Properties as CamelCase<K_1 & string>]: Properties[K_1][typeof COTYPE]; }>; | ||
var _b: <Properties extends Record<string, SchemaTypes>>(properties: Properties) => GroupConditional<Properties, { [K in keyof Properties]: Properties[K][typeof OTYPE]; }, { [K_1 in keyof Properties as CamelCase<K_1 & string>]: Properties[K_1][typeof COTYPE]; }>; | ||
export { _a as if, _b as else }; | ||
} |
@@ -28,3 +28,3 @@ import { RefsStore, RecordNode } from '@vinejs/compiler/types'; | ||
}>; | ||
validateKeys: (options: (keys: string[], ctx: import("@vinejs/compiler/types").FieldContext) => void) => Validation<(keys: string[], ctx: import("@vinejs/compiler/types").FieldContext) => void>; | ||
validateKeys: (options: (keys: string[], field: import("@vinejs/compiler/types").FieldContext) => void) => Validation<(keys: string[], field: import("@vinejs/compiler/types").FieldContext) => void>; | ||
}; | ||
@@ -31,0 +31,0 @@ [UNIQUE_NAME]: string; |
@@ -17,2 +17,2 @@ import { FieldContext } from '@vinejs/compiler/types'; | ||
}>; | ||
export declare const validateKeysRule: (options: (keys: string[], ctx: FieldContext) => void) => import("../../types.js").Validation<(keys: string[], ctx: FieldContext) => void>; | ||
export declare const validateKeysRule: (options: (keys: string[], field: FieldContext) => void) => import("../../types.js").Validation<(keys: string[], field: FieldContext) => void>; |
import { messages } from '../../defaults.js'; | ||
import { createRule } from '../../vine/create_rule.js'; | ||
export const minLengthRule = createRule((value, options, ctx) => { | ||
if (!ctx.isValid) { | ||
export const minLengthRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (Object.keys(value).length < options.min) { | ||
ctx.report(messages['record.minLength'], 'minLength', ctx, options); | ||
field.report(messages['record.minLength'], 'minLength', field, options); | ||
} | ||
}); | ||
export const maxLengthRule = createRule((value, options, ctx) => { | ||
if (!ctx.isValid) { | ||
export const maxLengthRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (Object.keys(value).length > options.max) { | ||
ctx.report(messages['record.maxLength'], 'maxLength', ctx, options); | ||
field.report(messages['record.maxLength'], 'maxLength', field, options); | ||
} | ||
}); | ||
export const fixedLengthRule = createRule((value, options, ctx) => { | ||
if (!ctx.isValid) { | ||
export const fixedLengthRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (Object.keys(value).length !== options.size) { | ||
ctx.report(messages['record.fixedLength'], 'fixedLength', ctx, options); | ||
field.report(messages['record.fixedLength'], 'fixedLength', field, options); | ||
} | ||
}); | ||
export const validateKeysRule = createRule((value, callback, ctx) => { | ||
if (!ctx.isValid) { | ||
export const validateKeysRule = createRule((value, callback, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
callback(Object.keys(value), ctx); | ||
callback(Object.keys(value), field); | ||
}); |
import { BaseLiteralType } from '../base/literal.js'; | ||
import { IS_OF_TYPE, UNIQUE_NAME } from '../../symbols.js'; | ||
import type { FieldOptions, Validation } from '../../types.js'; | ||
import { emailRule, mobileRule, urlRule } from './rules.js'; | ||
import type { Validation, AlphaOptions, FieldContext, FieldOptions, AlphaNumericOptions, NormalizeEmailOptions } from '../../types.js'; | ||
import { urlRule, emailRule, mobileRule } from './rules.js'; | ||
export declare class VineString extends BaseLiteralType<string, string> { | ||
static rules: { | ||
in: (options: { | ||
choices: string[] | ((field: FieldContext) => string[]); | ||
}) => Validation<{ | ||
choices: string[] | ((field: FieldContext) => string[]); | ||
}>; | ||
url: (options?: import("validator/lib/isURL.js").IsURLOptions | undefined) => Validation<import("validator/lib/isURL.js").IsURLOptions | undefined>; | ||
trim: (options?: undefined) => Validation<undefined>; | ||
email: (options?: import("validator/lib/isEmail.js").IsEmailOptions | undefined) => Validation<import("validator/lib/isEmail.js").IsEmailOptions | undefined>; | ||
alpha: (options?: AlphaOptions | undefined) => Validation<AlphaOptions | undefined>; | ||
notIn: (options: { | ||
list: string[] | ((field: FieldContext) => string[]); | ||
}) => Validation<{ | ||
list: string[] | ((field: FieldContext) => string[]); | ||
}>; | ||
regex: (options: RegExp) => Validation<RegExp>; | ||
sameAs: (options: { | ||
otherField: string; | ||
}) => Validation<{ | ||
otherField: string; | ||
}>; | ||
mobile: (options?: import("../../types.js").MobileOptions | ((field: FieldContext) => import("../../types.js").MobileOptions | undefined) | undefined) => Validation<import("../../types.js").MobileOptions | ((field: FieldContext) => import("../../types.js").MobileOptions | undefined) | undefined>; | ||
string: (options?: undefined) => Validation<undefined>; | ||
mobile: (options?: import("../../types.js").MobileOptions | ((ctx: import("@vinejs/compiler/types").FieldContext) => import("../../types.js").MobileOptions | undefined) | undefined) => Validation<import("../../types.js").MobileOptions | ((ctx: import("@vinejs/compiler/types").FieldContext) => import("../../types.js").MobileOptions | undefined) | undefined>; | ||
hexCode: (options?: undefined) => Validation<undefined>; | ||
endsWith: (options: { | ||
substring: string; | ||
}) => Validation<{ | ||
substring: string; | ||
}>; | ||
confirmed: (options?: { | ||
confirmationField: string; | ||
} | undefined) => Validation<{ | ||
confirmationField: string; | ||
} | undefined>; | ||
activeUrl: (options?: undefined) => Validation<undefined>; | ||
minLength: (options: { | ||
min: number; | ||
}) => Validation<{ | ||
min: number; | ||
}>; | ||
notSameAs: (options: { | ||
otherField: string; | ||
}) => Validation<{ | ||
otherField: string; | ||
}>; | ||
maxLength: (options: { | ||
max: number; | ||
}) => Validation<{ | ||
max: number; | ||
}>; | ||
ipAddress: (options?: { | ||
version: 4 | 6; | ||
} | undefined) => Validation<{ | ||
version: 4 | 6; | ||
} | undefined>; | ||
startsWith: (options: { | ||
substring: string; | ||
}) => Validation<{ | ||
substring: string; | ||
}>; | ||
fixedLength: (options: { | ||
size: number; | ||
}) => Validation<{ | ||
size: number; | ||
}>; | ||
alphaNumeric: (options?: AlphaOptions | undefined) => Validation<AlphaOptions | undefined>; | ||
normalizeEmail: (options?: import("validator").default.NormalizeEmailOptions | undefined) => Validation<import("validator").default.NormalizeEmailOptions | undefined>; | ||
}; | ||
@@ -17,6 +78,25 @@ [UNIQUE_NAME]: string; | ||
url(...args: Parameters<typeof urlRule>): this; | ||
activeUrl(): this; | ||
email(...args: Parameters<typeof emailRule>): this; | ||
mobile(...args: Parameters<typeof mobileRule>): this; | ||
ipAddress(version?: 4 | 6): this; | ||
hexCode(): this; | ||
regex(expression: RegExp): this; | ||
alpha(options?: AlphaOptions): this; | ||
alphaNumeric(options?: AlphaNumericOptions): this; | ||
minLength(expectedLength: number): this; | ||
maxLength(expectedLength: number): this; | ||
fixedLength(expectedLength: number): this; | ||
confirmed(options?: { | ||
confirmationField: string; | ||
}): this; | ||
trim(): this; | ||
normalizeEmail(options?: NormalizeEmailOptions): this; | ||
startsWith(substring: string): this; | ||
endsWith(substring: string): this; | ||
sameAs(otherField: string): this; | ||
notSameAs(otherField: string): this; | ||
in(choices: string[] | ((field: FieldContext) => string[])): this; | ||
notIn(list: string[] | ((field: FieldContext) => string[])): this; | ||
clone(): this; | ||
} |
import { BaseLiteralType } from '../base/literal.js'; | ||
import { IS_OF_TYPE, UNIQUE_NAME } from '../../symbols.js'; | ||
import { emailRule, hexCodeRule, mobileRule, stringRule, urlRule } from './rules.js'; | ||
import { inRule, urlRule, trimRule, alphaRule, emailRule, notInRule, regexRule, sameAsRule, mobileRule, stringRule, hexCodeRule, endsWithRule, ipAddressRule, confirmedRule, notSameAsRule, activeUrlRule, minLengthRule, maxLengthRule, startsWithRule, fixedLengthRule, alphaNumericRule, normalizeEmailRule, } from './rules.js'; | ||
export class VineString extends BaseLiteralType { | ||
static rules = { | ||
in: inRule, | ||
url: urlRule, | ||
trim: trimRule, | ||
email: emailRule, | ||
alpha: alphaRule, | ||
notIn: notInRule, | ||
regex: regexRule, | ||
sameAs: sameAsRule, | ||
mobile: mobileRule, | ||
string: stringRule, | ||
mobile: mobileRule, | ||
hexCode: hexCodeRule, | ||
endsWith: endsWithRule, | ||
confirmed: confirmedRule, | ||
activeUrl: activeUrlRule, | ||
minLength: minLengthRule, | ||
notSameAs: notSameAsRule, | ||
maxLength: maxLengthRule, | ||
ipAddress: ipAddressRule, | ||
startsWith: startsWithRule, | ||
fixedLength: fixedLengthRule, | ||
alphaNumeric: alphaNumericRule, | ||
normalizeEmail: normalizeEmailRule, | ||
}; | ||
@@ -22,2 +39,5 @@ [UNIQUE_NAME] = 'vine.string'; | ||
} | ||
activeUrl() { | ||
return this.use(activeUrlRule()); | ||
} | ||
email(...args) { | ||
@@ -29,5 +49,53 @@ return this.use(emailRule(...args)); | ||
} | ||
ipAddress(version) { | ||
return this.use(ipAddressRule(version ? { version } : undefined)); | ||
} | ||
hexCode() { | ||
return this.use(hexCodeRule()); | ||
} | ||
regex(expression) { | ||
return this.use(regexRule(expression)); | ||
} | ||
alpha(options) { | ||
return this.use(alphaRule(options)); | ||
} | ||
alphaNumeric(options) { | ||
return this.use(alphaNumericRule(options)); | ||
} | ||
minLength(expectedLength) { | ||
return this.use(minLengthRule({ min: expectedLength })); | ||
} | ||
maxLength(expectedLength) { | ||
return this.use(maxLengthRule({ max: expectedLength })); | ||
} | ||
fixedLength(expectedLength) { | ||
return this.use(fixedLengthRule({ size: expectedLength })); | ||
} | ||
confirmed(options) { | ||
return this.use(confirmedRule(options)); | ||
} | ||
trim() { | ||
return this.use(trimRule()); | ||
} | ||
normalizeEmail(options) { | ||
return this.use(normalizeEmailRule(options)); | ||
} | ||
startsWith(substring) { | ||
return this.use(startsWithRule({ substring })); | ||
} | ||
endsWith(substring) { | ||
return this.use(endsWithRule({ substring })); | ||
} | ||
sameAs(otherField) { | ||
return this.use(sameAsRule({ otherField })); | ||
} | ||
notSameAs(otherField) { | ||
return this.use(notSameAsRule({ otherField })); | ||
} | ||
in(choices) { | ||
return this.use(inRule({ choices })); | ||
} | ||
notIn(list) { | ||
return this.use(notInRule({ list })); | ||
} | ||
clone() { | ||
@@ -34,0 +102,0 @@ return new VineString(this.cloneOptions(), this.cloneValidations()); |
@@ -0,7 +1,69 @@ | ||
import validator from 'validator'; | ||
import type { FieldContext } from '@vinejs/compiler/types'; | ||
import type { MobileOptions } from '../../types.js'; | ||
import type { AlphaOptions, MobileOptions } from '../../types.js'; | ||
export declare const stringRule: (options?: undefined) => import("../../types.js").Validation<undefined>; | ||
export declare const emailRule: (options?: import("validator/lib/isEmail.js").IsEmailOptions | undefined) => import("../../types.js").Validation<import("validator/lib/isEmail.js").IsEmailOptions | undefined>; | ||
export declare const mobileRule: (options?: MobileOptions | ((ctx: FieldContext) => MobileOptions | undefined) | undefined) => import("../../types.js").Validation<MobileOptions | ((ctx: FieldContext) => MobileOptions | undefined) | undefined>; | ||
export declare const mobileRule: (options?: MobileOptions | ((field: FieldContext) => MobileOptions | undefined) | undefined) => import("../../types.js").Validation<MobileOptions | ((field: FieldContext) => MobileOptions | undefined) | undefined>; | ||
export declare const ipAddressRule: (options?: { | ||
version: 4 | 6; | ||
} | undefined) => import("../../types.js").Validation<{ | ||
version: 4 | 6; | ||
} | undefined>; | ||
export declare const regexRule: (options: RegExp) => import("../../types.js").Validation<RegExp>; | ||
export declare const hexCodeRule: (options?: undefined) => import("../../types.js").Validation<undefined>; | ||
export declare const urlRule: (options?: import("validator/lib/isURL.js").IsURLOptions | undefined) => import("../../types.js").Validation<import("validator/lib/isURL.js").IsURLOptions | undefined>; | ||
export declare const activeUrlRule: (options?: undefined) => import("../../types.js").Validation<undefined>; | ||
export declare const alphaRule: (options?: AlphaOptions | undefined) => import("../../types.js").Validation<AlphaOptions | undefined>; | ||
export declare const alphaNumericRule: (options?: AlphaOptions | undefined) => import("../../types.js").Validation<AlphaOptions | undefined>; | ||
export declare const minLengthRule: (options: { | ||
min: number; | ||
}) => import("../../types.js").Validation<{ | ||
min: number; | ||
}>; | ||
export declare const maxLengthRule: (options: { | ||
max: number; | ||
}) => import("../../types.js").Validation<{ | ||
max: number; | ||
}>; | ||
export declare const fixedLengthRule: (options: { | ||
size: number; | ||
}) => import("../../types.js").Validation<{ | ||
size: number; | ||
}>; | ||
export declare const endsWithRule: (options: { | ||
substring: string; | ||
}) => import("../../types.js").Validation<{ | ||
substring: string; | ||
}>; | ||
export declare const startsWithRule: (options: { | ||
substring: string; | ||
}) => import("../../types.js").Validation<{ | ||
substring: string; | ||
}>; | ||
export declare const sameAsRule: (options: { | ||
otherField: string; | ||
}) => import("../../types.js").Validation<{ | ||
otherField: string; | ||
}>; | ||
export declare const notSameAsRule: (options: { | ||
otherField: string; | ||
}) => import("../../types.js").Validation<{ | ||
otherField: string; | ||
}>; | ||
export declare const confirmedRule: (options?: { | ||
confirmationField: string; | ||
} | undefined) => import("../../types.js").Validation<{ | ||
confirmationField: string; | ||
} | undefined>; | ||
export declare const trimRule: (options?: undefined) => import("../../types.js").Validation<undefined>; | ||
export declare const normalizeEmailRule: (options?: validator.default.NormalizeEmailOptions | undefined) => import("../../types.js").Validation<validator.default.NormalizeEmailOptions | undefined>; | ||
export declare const inRule: (options: { | ||
choices: string[] | ((field: FieldContext) => string[]); | ||
}) => import("../../types.js").Validation<{ | ||
choices: string[] | ((field: FieldContext) => string[]); | ||
}>; | ||
export declare const notInRule: (options: { | ||
list: string[] | ((field: FieldContext) => string[]); | ||
}) => import("../../types.js").Validation<{ | ||
list: string[] | ((field: FieldContext) => string[]); | ||
}>; |
@@ -0,42 +1,212 @@ | ||
import validator from 'validator'; | ||
import { helpers } from '../../vine/helpers.js'; | ||
import { messages } from '../../defaults.js'; | ||
import { createRule } from '../../vine/create_rule.js'; | ||
export const stringRule = createRule((value, _, ctx) => { | ||
export const stringRule = createRule((value, _, field) => { | ||
if (typeof value !== 'string') { | ||
ctx.report(messages.string, 'string', ctx); | ||
field.report(messages.string, 'string', field); | ||
} | ||
}); | ||
export const emailRule = createRule((value, options, ctx) => { | ||
if (!ctx.isValid) { | ||
export const emailRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (!helpers.isEmail(value, options)) { | ||
ctx.report(messages.email, 'email', ctx); | ||
field.report(messages.email, 'email', field); | ||
} | ||
}); | ||
export const mobileRule = createRule((value, options, ctx) => { | ||
if (!ctx.isValid) { | ||
export const mobileRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
const normalizedOptions = options && typeof options === 'function' ? options(ctx) : options; | ||
const normalizedOptions = options && typeof options === 'function' ? options(field) : options; | ||
const locales = normalizedOptions?.locales || 'any'; | ||
if (!helpers.isMobilePhone(value, locales, normalizedOptions)) { | ||
ctx.report(messages.mobile, 'mobile', ctx); | ||
field.report(messages.mobile, 'mobile', field); | ||
} | ||
}); | ||
export const hexCodeRule = createRule((value, _, ctx) => { | ||
if (!ctx.isValid) { | ||
export const ipAddressRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (!helpers.isIP(value, options?.version)) { | ||
field.report(messages.ipAddress, 'ipAddress', field); | ||
} | ||
}); | ||
export const regexRule = createRule((value, expression, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (!expression.test(value)) { | ||
field.report(messages.regex, 'regex', field); | ||
} | ||
}); | ||
export const hexCodeRule = createRule((value, _, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (!helpers.isHexColor(value)) { | ||
ctx.report(messages.hexCode, 'hexCode', ctx); | ||
field.report(messages.hexCode, 'hexCode', field); | ||
} | ||
}); | ||
export const urlRule = createRule((value, options, ctx) => { | ||
if (!ctx.isValid) { | ||
export const urlRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (!helpers.isURL(value, options)) { | ||
ctx.report(messages.url, 'url', ctx); | ||
field.report(messages.url, 'url', field); | ||
} | ||
}); | ||
export const activeUrlRule = createRule(async (value, _, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (!(await helpers.isActiveURL(value))) { | ||
field.report(messages.activeUrl, 'activeUrl', field); | ||
} | ||
}); | ||
export const alphaRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
let characterSet = 'a-zA-Z'; | ||
if (options) { | ||
if (options.allowSpaces) { | ||
characterSet += '\\s'; | ||
} | ||
if (options.allowDashes) { | ||
characterSet += '-'; | ||
} | ||
if (options.allowUnderscores) { | ||
characterSet += '_'; | ||
} | ||
} | ||
const expression = new RegExp(`^[${characterSet}]+$`); | ||
if (!expression.test(value)) { | ||
field.report(messages.alpha, 'alpha', field); | ||
} | ||
}); | ||
export const alphaNumericRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
let characterSet = 'a-zA-Z0-9'; | ||
if (options) { | ||
if (options.allowSpaces) { | ||
characterSet += '\\s'; | ||
} | ||
if (options.allowDashes) { | ||
characterSet += '-'; | ||
} | ||
if (options.allowUnderscores) { | ||
characterSet += '_'; | ||
} | ||
} | ||
const expression = new RegExp(`^[${characterSet}]+$`); | ||
if (!expression.test(value)) { | ||
field.report(messages.alphaNumeric, 'alphaNumeric', field); | ||
} | ||
}); | ||
export const minLengthRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (value.length < options.min) { | ||
field.report(messages.minLength, 'minLength', field, options); | ||
} | ||
}); | ||
export const maxLengthRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (value.length > options.max) { | ||
field.report(messages.maxLength, 'maxLength', field, options); | ||
} | ||
}); | ||
export const fixedLengthRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (value.length !== options.size) { | ||
field.report(messages.fixedLength, 'fixedLength', field, options); | ||
} | ||
}); | ||
export const endsWithRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (!value.endsWith(options.substring)) { | ||
field.report(messages.endsWith, 'endsWith', field, options); | ||
} | ||
}); | ||
export const startsWithRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
if (!value.startsWith(options.substring)) { | ||
field.report(messages.startsWith, 'startsWith', field, options); | ||
} | ||
}); | ||
export const sameAsRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
const input = field.parent[options.otherField]; | ||
if (input !== value) { | ||
field.report(messages.sameAs, 'sameAs', field, options); | ||
return; | ||
} | ||
}); | ||
export const notSameAsRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
const input = field.parent[options.otherField]; | ||
if (input === value) { | ||
field.report(messages.notSameAs, 'notSameAs', field, options); | ||
return; | ||
} | ||
}); | ||
export const confirmedRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
const otherField = options?.confirmationField || `${field.name}_confirmation`; | ||
const input = field.parent[otherField]; | ||
if (input !== value) { | ||
field.report(messages.confirmed, 'confirmed', field, { otherField }); | ||
return; | ||
} | ||
}); | ||
export const trimRule = createRule((value, _, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
field.mutate(value.trim(), field); | ||
}); | ||
export const normalizeEmailRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
field.mutate(validator.default.normalizeEmail(value, options), field); | ||
}); | ||
export const inRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
const choices = typeof options.choices === 'function' ? options.choices(field) : options.choices; | ||
if (!choices.includes(value)) { | ||
field.report(messages.in, 'in', field, options); | ||
return; | ||
} | ||
}); | ||
export const notInRule = createRule((value, options, field) => { | ||
if (!field.isValid) { | ||
return; | ||
} | ||
const list = typeof options.list === 'function' ? options.list(field) : options.list; | ||
if (list.includes(value)) { | ||
field.report(messages.notIn, 'notIn', field, options); | ||
return; | ||
} | ||
}); |
@@ -30,4 +30,4 @@ import camelcase from 'camelcase'; | ||
return { | ||
conditionalFnRefId: refs.trackConditional((value, ctx) => { | ||
return schema[IS_OF_TYPE](value, ctx); | ||
conditionalFnRefId: refs.trackConditional((value, field) => { | ||
return schema[IS_OF_TYPE](value, field); | ||
}), | ||
@@ -34,0 +34,0 @@ schema: schema[PARSE](propertyName, refs, options), |
@@ -6,5 +6,5 @@ import { VineUnion } from './main.js'; | ||
export declare namespace union { | ||
var _a: <Schema extends SchemaTypes>(conditon: (value: Record<string, unknown>, ctx: FieldContext) => any, schema: Schema) => UnionConditional<Schema>; | ||
var _a: <Schema extends SchemaTypes>(conditon: (value: Record<string, unknown>, field: FieldContext) => any, schema: Schema) => UnionConditional<Schema>; | ||
var _b: <Schema extends SchemaTypes>(schema: Schema) => UnionConditional<Schema>; | ||
export { _a as if, _b as else }; | ||
} |
@@ -6,2 +6,3 @@ import type { ParseFn, RefsStore, TransformFn, FieldContext, CompilerNodes, MessagesProviderContact, ErrorReporterContract as BaseReporter } from '@vinejs/compiler/types'; | ||
import type { IsEmailOptions } from 'validator/lib/isEmail.js'; | ||
import type { NormalizeEmailOptions } from 'validator/lib/normalizeEmail.js'; | ||
import type { IsMobilePhoneOptions, MobilePhoneLocale } from 'validator/lib/isMobilePhone.js'; | ||
@@ -12,3 +13,10 @@ export type MobileOptions = { | ||
export type EmailOptions = IsEmailOptions; | ||
export { NormalizeEmailOptions }; | ||
export type URLOptions = IsURLOptions; | ||
export type AlphaOptions = { | ||
allowSpaces?: boolean; | ||
allowUnderscores?: boolean; | ||
allowDashes?: boolean; | ||
}; | ||
export type AlphaNumericOptions = AlphaOptions; | ||
export type { Refs, FieldContext, RefIdentifier, ConditionalFn, MessagesProviderContact, } from '@vinejs/compiler/types'; | ||
@@ -27,6 +35,6 @@ export type EnumLike = { | ||
[UNIQUE_NAME]?: string; | ||
[IS_OF_TYPE]?: (value: unknown, ctx: FieldContext) => boolean; | ||
[IS_OF_TYPE]?: (value: unknown, field: FieldContext) => boolean; | ||
} | ||
export type SchemaTypes = ConstructableSchema<any, any>; | ||
export type Validator<Options extends any> = (value: unknown, options: Options, ctx: FieldContext) => any | Promise<any>; | ||
export type Validator<Options extends any> = (value: unknown, options: Options, field: FieldContext) => any | Promise<any>; | ||
export type ValidationRule<Options extends any> = { | ||
@@ -55,3 +63,3 @@ validator: Validator<Options>; | ||
}; | ||
export type UnionNoMatchCallback<Input> = (value: Input, ctx: FieldContext) => any; | ||
export type UnionNoMatchCallback<Input> = (value: Input, field: FieldContext) => any; | ||
export interface ErrorReporterContract extends BaseReporter { | ||
@@ -58,0 +66,0 @@ createError(): ValidationError; |
@@ -33,4 +33,4 @@ import validator from 'validator'; | ||
isHexColor: (value: string) => boolean; | ||
isActiveURL: (url: string) => Promise<number | boolean>; | ||
isActiveURL: (url: string) => Promise<boolean>; | ||
isDistinct: (dataSet: any[], fields?: string | string[]) => boolean; | ||
}; |
@@ -81,4 +81,6 @@ import validator from 'validator'; | ||
} | ||
const v4Addresses = await resolve4(hostname); | ||
return v4Addresses.length; | ||
else { | ||
const v4Addresses = await resolve4(hostname); | ||
return v4Addresses.length > 0; | ||
} | ||
} | ||
@@ -85,0 +87,0 @@ catch { |
@@ -40,3 +40,3 @@ import { createRule } from './create_rule.js'; | ||
isHexColor: (value: string) => boolean; | ||
isActiveURL: (url: string) => Promise<number | boolean>; | ||
isActiveURL: (url: string) => Promise<boolean>; | ||
isDistinct: (dataSet: any[], fields?: string | string[] | undefined) => boolean; | ||
@@ -43,0 +43,0 @@ }; |
{ | ||
"name": "@vinejs/vine", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Form data validation library for Node.js", | ||
@@ -51,4 +51,4 @@ "type": "module", | ||
"@japa/spec-reporter": "^1.3.3", | ||
"@swc/core": "^1.3.62", | ||
"@types/node": "^20.3.0", | ||
"@swc/core": "^1.3.63", | ||
"@types/node": "^20.3.1", | ||
"benchmark": "^2.1.4", | ||
@@ -63,3 +63,3 @@ "c8": "^7.14.0", | ||
"husky": "^8.0.3", | ||
"np": "^8.0.3", | ||
"np": "^8.0.4", | ||
"prettier": "^2.8.7", | ||
@@ -130,3 +130,3 @@ "ts-node": "^10.9.1", | ||
"@types/validator": "^13.7.17", | ||
"@vinejs/compiler": "^1.2.2", | ||
"@vinejs/compiler": "^2.0.1", | ||
"camelcase": "^7.0.1", | ||
@@ -133,0 +133,0 @@ "validator": "^13.9.0" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
110249
2685
+ Added@vinejs/compiler@2.5.1(transitive)
- Removed@vinejs/compiler@1.2.2(transitive)
Updated@vinejs/compiler@^2.0.1