Skip to content

Commit 2d9b271

Browse files
committed
refactor: rename all references of ctx with field
1 parent b4ffb14 commit 2d9b271

File tree

5 files changed

+33
-33
lines changed

5 files changed

+33
-33
lines changed

benchmarks/flat_object.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ const data = { username: 'virk' }
1212
const meta = {}
1313
const refs = {
1414
'ref://1': {
15-
validator(value: unknown, _: any, ctx: any) {
15+
validator(value: unknown, _: any, field: any) {
1616
if (typeof value !== 'string') {
17-
ctx.report('Value is not a string')
17+
field.report('Value is not a string')
1818
}
1919
},
2020
options: {},

benchmarks/nested_object.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ const data = { profile: { username: 'virk' } }
1212
const meta = {}
1313
const refs = {
1414
'ref://1': {
15-
validator(value: unknown, _: any, ctx: any) {
15+
validator(value: unknown, _: any, field: any) {
1616
if (typeof value !== 'string') {
17-
ctx.report('Value is not a string')
17+
field.report('Value is not a string')
1818
}
1919
},
2020
options: {},

benchmarks/object_with_arrays.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ const data = { contacts: [{ email: '[email protected]' }] }
1212
const meta = {}
1313
const refs = {
1414
'ref://1': {
15-
validator(value: unknown, _: any, ctx: any) {
15+
validator(value: unknown, _: any, field: any) {
1616
if (typeof value !== 'string') {
17-
ctx.report('Value is not a string', ctx)
17+
field.report('Value is not a string', field)
1818
}
1919
},
2020
options: {},

src/scripts/define_inline_functions.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,48 @@
1212
* validation runtime code.
1313
*/
1414
export function defineInlineFunctions(options: { convertEmptyStringsToNull: boolean }) {
15-
return `function report(message, rule, ctx, args) {
16-
ctx.isValid = false;
17-
errorReporter.report(messagesProvider.getMessage(message, rule, ctx, args), rule, ctx, args);
15+
return `function report(message, rule, field, args) {
16+
field.isValid = false;
17+
errorReporter.report(messagesProvider.getMessage(message, rule, field, args), rule, field, args);
1818
};
19-
function defineValue(value, ctx) {
19+
function defineValue(value, field) {
2020
${options.convertEmptyStringsToNull ? `if (value === '') { value = null; }` : ''}
21-
ctx.value = value;
22-
ctx.isDefined = value !== undefined && value !== null;
23-
return ctx;
21+
field.value = value;
22+
field.isDefined = value !== undefined && value !== null;
23+
return field;
2424
};
25-
function ensureExists(ctx) {
26-
if (ctx.value === undefined || ctx.value === null) {
27-
ctx.report(REQUIRED, 'required', ctx);
25+
function ensureExists(field) {
26+
if (field.value === undefined || field.value === null) {
27+
field.report(REQUIRED, 'required', field);
2828
return false;
2929
}
3030
return true;
3131
};
32-
function ensureIsDefined(ctx) {
33-
if (ctx.value === undefined) {
34-
ctx.report(REQUIRED, 'required', ctx);
32+
function ensureIsDefined(field) {
33+
if (field.value === undefined) {
34+
field.report(REQUIRED, 'required', field);
3535
return false;
3636
}
3737
return true;
3838
};
39-
function ensureIsObject(ctx) {
40-
if (!ctx.isDefined) {
39+
function ensureIsObject(field) {
40+
if (!field.isDefined) {
4141
return false;
4242
}
43-
if (typeof ctx.value == 'object' && !Array.isArray(ctx.value)) {
43+
if (typeof field.value == 'object' && !Array.isArray(field.value)) {
4444
return true;
4545
}
46-
ctx.report(NOT_AN_OBJECT, 'object', ctx);
46+
field.report(NOT_AN_OBJECT, 'object', field);
4747
return false;
4848
};
49-
function ensureIsArray(ctx) {
50-
if (!ctx.isDefined) {
49+
function ensureIsArray(field) {
50+
if (!field.isDefined) {
5151
return false;
5252
}
53-
if (Array.isArray(ctx.value)) {
53+
if (Array.isArray(field.value)) {
5454
return true;
5555
}
56-
ctx.report(NOT_AN_ARRAY, 'array', ctx);
56+
field.report(NOT_AN_ARRAY, 'array', field);
5757
return false;
5858
};
5959
function copyProperties(val) {

src/types.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export type FieldContext = {
7777
/**
7878
* Mutate the value of field under validation.
7979
*/
80-
mutate(newValue: any, ctx: FieldContext): void
80+
mutate(newValue: any, field: FieldContext): void
8181

8282
/**
8383
* Report error to the error reporter
@@ -128,7 +128,7 @@ export type ValidationRule = {
128128
/**
129129
* Performs validation
130130
*/
131-
validator(value: unknown, options: any, ctx: FieldContext): any
131+
validator(value: unknown, options: any, field: FieldContext): any
132132

133133
/**
134134
* Options to pass
@@ -144,12 +144,12 @@ export type ParseFn = (value: unknown) => any
144144
/**
145145
* The shape of transform function picked from the refs
146146
*/
147-
export type TransformFn<Input, Output> = (value: Input, ctx: FieldContext) => Output
147+
export type TransformFn<Input, Output> = (value: Input, field: FieldContext) => Output
148148

149149
/**
150150
* The shape of conditional function used for narrowing down unions.
151151
*/
152-
export type ConditionalFn<Input> = (value: Input, ctx: FieldContext) => boolean
152+
export type ConditionalFn<Input> = (value: Input, field: FieldContext) => boolean
153153

154154
/**
155155
* Shape of a validation rule accepted by the compiler
@@ -475,7 +475,7 @@ export interface ErrorReporterContract {
475475
/**
476476
* Report error for a field
477477
*/
478-
report(message: string, rule: string, ctx: FieldContext, args?: Record<string, any>): any
478+
report(message: string, rule: string, field: FieldContext, args?: Record<string, any>): any
479479
}
480480

481481
/**
@@ -490,7 +490,7 @@ export interface MessagesProviderContact {
490490
getMessage(
491491
defaultMessage: string,
492492
rule: string,
493-
ctx: FieldContext,
493+
field: FieldContext,
494494
args?: Record<string, any>
495495
): string
496496
}

0 commit comments

Comments
 (0)