Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: expose FieldContext type closes #3398
  • Loading branch information
logaretm committed Jul 20, 2021
1 parent 6034e66 commit a6e4c0a
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions packages/i18n/src/index.ts
@@ -1,4 +1,4 @@
import { isCallable, FieldContext, ValidationMessageGenerator } from '../../shared';
import { isCallable, FieldValidationMetaInfo, ValidationMessageGenerator } from '../../shared';
import { interpolate, merge } from './utils';

type ValidationMessageTemplate = ValidationMessageGenerator | string;
Expand All @@ -23,11 +23,11 @@ class Dictionary {
this.merge(dictionary);
}

public resolve(ctx: FieldContext) {
public resolve(ctx: FieldValidationMetaInfo) {
return this.format(this.locale, ctx);
}

public format(locale: string, ctx: FieldContext) {
public format(locale: string, ctx: FieldValidationMetaInfo) {
let message!: ValidationMessageTemplate | undefined;
const { field, rule, form } = ctx;
if (!rule) {
Expand Down
6 changes: 3 additions & 3 deletions packages/shared/types.ts
@@ -1,4 +1,4 @@
export interface FieldContext {
export interface FieldValidationMetaInfo {
field: string;
value: unknown;
form: Record<string, unknown>;
Expand All @@ -11,12 +11,12 @@ export interface FieldContext {
export type ValidationRuleFunction<TValue = unknown, TParams = unknown[] | Record<string, unknown>> = (
value: TValue,
params: TParams,
ctx: FieldContext
ctx: FieldValidationMetaInfo
) => boolean | string | Promise<boolean | string>;

export type SimpleValidationRuleFunction<TValue = unknown, TParams = unknown[] | Record<string, unknown>> = (
value: TValue,
params: TParams
) => boolean | string | Promise<boolean | string>;

export type ValidationMessageGenerator = (ctx: FieldContext) => string;
export type ValidationMessageGenerator = (ctx: FieldValidationMetaInfo) => string;
4 changes: 2 additions & 2 deletions packages/vee-validate/src/index.ts
Expand Up @@ -11,8 +11,8 @@ export {
FormActions,
FormState,
FormValidationResult,
FormContext,
PublicFormContext,
PublicFormContext as FormContext,
FieldContext,
SubmissionContext,
SubmissionHandler,
} from './types';
Expand Down
6 changes: 3 additions & 3 deletions packages/vee-validate/src/types.ts
@@ -1,6 +1,6 @@
import { ComputedRef, Ref, WritableComputedRef } from 'vue';
import { SchemaOf, AnySchema, AnyObjectSchema } from 'yup';
import { FieldContext } from '../../shared';
import { FieldValidationMetaInfo } from '../../shared';

export interface ValidationResult {
errors: string[];
Expand Down Expand Up @@ -63,11 +63,11 @@ export interface PrivateFieldComposite<TValue = unknown> {
setErrors(message: string | string[]): void;
}

export type FieldComposable<TValue = unknown> = Omit<PrivateFieldComposite<TValue>, 'idx' | 'fid'>;
export type FieldContext<TValue = unknown> = Omit<PrivateFieldComposite<TValue>, 'idx' | 'fid'>;

export type GenericValidateFunction = (
value: unknown,
ctx: FieldContext
ctx: FieldValidationMetaInfo
) => boolean | string | Promise<boolean | string>;

export interface FormState<TValues> {
Expand Down
4 changes: 2 additions & 2 deletions packages/vee-validate/src/useField.ts
Expand Up @@ -22,7 +22,7 @@ import {
GenericValidateFunction,
FieldMeta,
YupValidator,
FieldComposable,
FieldContext,
FieldState,
PrivateFieldComposite,
WritableRef,
Expand Down Expand Up @@ -71,7 +71,7 @@ export function useField<TValue = unknown>(
name: MaybeRef<string>,
rules?: MaybeRef<RuleExpression<TValue>>,
opts?: Partial<FieldOptions<TValue>>
): FieldComposable<TValue> {
): FieldContext<TValue> {
const fid = ID_COUNTER >= Number.MAX_SAFE_INTEGER ? 0 : ++ID_COUNTER;
const {
initialValue,
Expand Down
6 changes: 3 additions & 3 deletions packages/vee-validate/src/validate.ts
Expand Up @@ -3,7 +3,7 @@ import { resolveRule } from './defineRule';
import { isLocator, normalizeRules, isYupValidator, keysOf, getFromPath } from './utils';
import { getConfig } from './config';
import { ValidationResult, GenericValidateFunction, YupValidator, FormValidationResult, RawFormSchema } from './types';
import { isCallable, FieldContext } from '../../shared';
import { isCallable, FieldValidationMetaInfo } from '../../shared';

/**
* Used internally
Expand Down Expand Up @@ -143,7 +143,7 @@ async function _test(
}

const params = fillTargetValues(rule.params, field.formData);
const ctx: FieldContext = {
const ctx: FieldValidationMetaInfo = {
field: field.name,
value,
form: field.formData,
Expand All @@ -169,7 +169,7 @@ async function _test(
/**
* Generates error messages.
*/
function _generateFieldError(fieldCtx: FieldContext) {
function _generateFieldError(fieldCtx: FieldValidationMetaInfo) {
const message = getConfig().generateMessage;
if (!message) {
return 'Field is invalid';
Expand Down
4 changes: 2 additions & 2 deletions packages/vee-validate/tests/useForm.spec.ts
@@ -1,5 +1,5 @@
import flushPromises from 'flush-promises';
import { PublicFormContext, useField, useForm } from '@/vee-validate';
import { FormContext, useField, useForm } from '@/vee-validate';
import { mountWithHoc, setValue } from './helpers';
import * as yup from 'yup';

Expand Down Expand Up @@ -32,7 +32,7 @@ describe('useForm()', () => {
});

test('can clear individual field error messages', async () => {
let setFieldError!: PublicFormContext['setFieldError'];
let setFieldError!: FormContext['setFieldError'];
mountWithHoc({
setup() {
const form = useForm();
Expand Down

0 comments on commit a6e4c0a

Please sign in to comment.