Skip to content

Commit d6c9564

Browse files
authored
fix(vfg): typescript clean up [khcp-11325] (#1900)
Convert files to use setup syntax/cleanup ts. Part of [KHCP-11325](https://konghq.atlassian.net/browse/KHCP-11325).
1 parent defe773 commit d6c9564

File tree

8 files changed

+226
-157
lines changed

8 files changed

+226
-157
lines changed

packages/core/forms/src/components/fields/abstractField.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import isString from 'lodash-es/isString'
88
import arrayUniq from 'lodash-es/uniq'
99
import uniqueId from 'lodash-es/uniqueId'
1010
import { slugifyFormID } from '../../utils/schema'
11-
import validators from '../../utils/validators'
11+
import { validators } from '../../utils/validators'
1212

1313
function convertValidator(validator) {
1414
if (isString(validator)) {

packages/core/forms/src/composables/useAbstractFields.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import isFunction from 'lodash-es/isFunction'
77
import isString from 'lodash-es/isString'
88
import arrayUniq from 'lodash-es/uniq'
99
import uniqueId from 'lodash-es/uniqueId'
10-
import validators from '../utils/validators'
10+
import { validators } from '../utils/validators'
1111
import { slugifyFormID } from '../utils/schema'
1212

1313
interface AbstractFieldParams {

packages/core/forms/src/locales/en.json

+23
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@
109109
"address_example": "e.g. localhost:6379"
110110
}
111111
},
112+
"validators": {
113+
"field_is_required": "This field is required",
114+
"invalid_format": "Invalid format",
115+
"number_too_small": "The number is too small. Minimum: {0}",
116+
"number_too_large": "The number is too large. Maximum: {0}",
117+
"invalid_number": "Invalid number",
118+
"invalid_integer": "The value is not an integer",
119+
"text_too_short": "The length of text is too short. Current: {0}, Minimum: {1}",
120+
"text_too_long": "The length of text is too long. Current: {0}, Maximum: {1}",
121+
"this_not_text": "This is not a text",
122+
"this_not_array": "This is not an array",
123+
"select_min_items": "Select minimum {0} items",
124+
"select_max_items": "Select maximum {0} items",
125+
"invalid_date": "Invalid date",
126+
"date_is_early": "The date is too early. Current: {0}, Minimum: {1}",
127+
"date_is_late": "The date is too late. Current: {0}, Maximum: {1}",
128+
"invalid_email": "Invalid e-mail address",
129+
"invalid_url": "Invalid URL",
130+
"invalid_card": "Invalid card format",
131+
"invalid_card_number": "Invalid card number",
132+
"invalid_text_contain_number": "Invalid text - cannot contains numbers or special characters",
133+
"invalid_tex_contain_spec": "Invalid text - cannot contains special characters"
134+
},
112135
"vfg": {
113136
"labels": {
114137
"on": "On",

packages/core/forms/src/types/form-generator.ts

+5
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ export interface AutofillSlotProps {
2424
}
2525

2626
export type AutofillSlot = Slot<AutofillSlotProps>
27+
28+
export type Validator = (value: any, field: any, model: any, messages: Record<string, string>) => string[]
29+
export interface Validators {
30+
[key: string]: Validator
31+
}

packages/core/forms/src/utils/dateFieldHelper.ts

-26
This file was deleted.

packages/core/forms/src/utils/isValidUuid.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
*/
66
export const uuidRegEx = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
77

8-
export function isValidUuid(str: string) {
9-
if (!str) return false
8+
export const isValidUuid = (str: string) => {
9+
if (!str) {
10+
return false
11+
}
1012

1113
return str.length === 36 && new RegExp(`^${uuidRegEx}$`).test(str)
1214
}

packages/core/forms/src/utils/schema.ts

+30-25
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import isObject from 'lodash-es/isObject'
66
import set from 'lodash-es/set'
77

88
// Create a new model by schema default values
9-
export const createDefaultObject = (schema: any, obj: Record<string, any> = {}) => {
9+
export const createDefaultObject = (schema: any, obj: Record<string, any> = {}): Record<string, any> => {
1010
each(schema.fields, (field: any) => {
1111
if (get(obj, field.model) === undefined && field.default !== undefined) {
1212
if (isFunction(field.default)) {
@@ -16,22 +16,26 @@ export const createDefaultObject = (schema: any, obj: Record<string, any> = {})
1616
} else set(obj, field.model, field.default)
1717
}
1818
})
19+
1920
return obj
2021
}
2122

2223
// Get a new model which contains only properties of multi-edit fields
23-
export const getMultipleFields = (schema: any) => {
24-
const res: any = []
24+
export const getMultipleFields = (schema: any): any[] => {
25+
const res: any[] = []
26+
2527
each(schema.fields, field => {
26-
if (field.multi === true) res.push(field)
28+
if (field.multi === true) {
29+
res.push(field)
30+
}
2731
})
2832

2933
return res
3034
}
3135

3236
// Merge many models to one 'work model' by schema
33-
export const mergeMultiObjectFields = (schema: any, objs: any) => {
34-
const model = {}
37+
export const mergeMultiObjectFields = (schema: any, objs: any): Record<string, any> => {
38+
const model: Record<string, any> = {}
3539

3640
const fields = getMultipleFields(schema)
3741

@@ -41,11 +45,12 @@ export const mergeMultiObjectFields = (schema: any, objs: any) => {
4145
const path = field.model
4246

4347
each(objs, (obj: any) => {
44-
const v = get(obj, path)
48+
const val = get(obj, path)
49+
4550
if (notSet) {
46-
mergedValue = v
51+
mergedValue = val
4752
notSet = false
48-
} else if (mergedValue !== v) {
53+
} else if (mergedValue !== val) {
4954
mergedValue = undefined
5055
}
5156
})
@@ -56,50 +61,50 @@ export const mergeMultiObjectFields = (schema: any, objs: any) => {
5661
return model
5762
}
5863

59-
export const slugifyFormID = (schema: any, prefix: any = '') => {
64+
export const slugifyFormID = (schema: any, prefix: any = ''): string => {
6065
// Try to get a reasonable default id from the schema,
6166
// then slugify it.
6267
if (typeof schema.id !== 'undefined') {
6368
// If an ID's been explicitly set, use it unchanged
64-
return prefix + schema.id
69+
return prefix + schema.id + ''
6570
} else {
6671
// Return the slugified version of either:
6772
return (
6873
prefix +
6974
(schema.inputName || schema.label || schema.model || '')
70-
// NB: This is a very simple, conservative, slugify function,
71-
// avoiding extra dependencies.
75+
// NB: This is a very simple, conservative, slugify function,
76+
// avoiding extra dependencies.
7277
.toString()
7378
.trim()
7479
.toLowerCase()
75-
// Spaces & underscores to dashes
80+
// Spaces & underscores to dashes
7681
.replace(/ |_/g, '-')
77-
// Multiple dashes to one
82+
// Multiple dashes to one
7883
.replace(/-{2,}/g, '-')
79-
// Remove leading & trailing dashes
84+
// Remove leading & trailing dashes
8085
.replace(/^-+|-+$/g, '')
81-
// Remove anything that isn't a (English/ASCII) letter, number or dash.
86+
// Remove anything that isn't a (English/ASCII) letter, number or dash.
8287
.replace(/([^a-zA-Z0-9-]+)/g, '')
8388
)
8489
}
8590
}
8691

87-
export const slugify = (name: any = '') => {
92+
export const slugify = (name: any = ''): string => {
8893
// Return the slugified version of either:
8994
return (
9095
name
91-
// NB: This is a very simple, conservative, slugify function,
92-
// avoiding extra dependencies.
96+
// NB: This is a very simple, conservative, slugify function,
97+
// avoiding extra dependencies.
9398
.toString()
9499
.trim()
95-
// .toLowerCase()
96-
// Spaces to dashes
100+
// .toLowerCase()
101+
// Spaces to dashes
97102
.replace(/ /g, '-')
98-
// Multiple dashes to one
103+
// Multiple dashes to one
99104
.replace(/-{2,}/g, '-')
100-
// Remove leading & trailing dashes
105+
// Remove leading & trailing dashes
101106
.replace(/^-+|-+$/g, '')
102-
// Remove anything that isn't a (English/ASCII) letter, number or dash.
107+
// Remove anything that isn't a (English/ASCII) letter, number or dash.
103108
.replace(/([^a-zA-Z0-9-_/./:]+)/g, '')
104109
)
105110
}

0 commit comments

Comments
 (0)