Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
19 changes: 15 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"swagger-autogen": "2.23.7",
"swagger-ui-express": "5.0.1",
"uuid": "10.0.0",
"winston": "3.17.0"
"winston": "3.17.0",
"yaml": "^2.3.4"
},
"devDependencies": {
"@types/cors": "2.8.17",
Expand All @@ -53,7 +54,7 @@
"ts-node": "10.9.2",
"tsc-alias": "1.8.16",
"tsconfig-paths": "4.2.0",
"typescript": "5.4.5",
"typescript": "^5.4.5",
"typescript-eslint": "8.1.0"
}
}
105 changes: 105 additions & 0 deletions src/endpoints/forms/formSync/utils/multilineHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import * as yaml from 'yaml'

/**
* Processes multiline strings in YAML to ensure they are properly formatted
* @param yamlContent - The YAML content as string
* @returns Processed YAML content with proper multiline formatting
*/
export const processMultilineInYaml = (yamlContent: string): string => {
try {
// Parse the YAML to get the object structure
const parsed = yaml.parse(yamlContent)

// Convert back to YAML with proper multiline formatting
const processed = yaml.stringify(parsed, {
// Use literal block scalar (|) for multiline strings
blockQuote: 'literal',
// Preserve line breaks
lineWidth: 0,
// Use double quotes for strings that need escaping
doubleQuotedAsJSON: false,
// Use literal block scalar for multiline strings
defaultStringType: 'QUOTE_DOUBLE',
})

return processed
} catch (error) {
// If parsing fails, return original content
console.warn('Failed to process multiline YAML:', error)
return yamlContent
}
}

/**
* Processes form values to ensure multiline strings are properly handled
* @param values - The form values object
* @returns Processed values with multiline strings properly formatted
*/
export const processMultilineInFormValues = (values: any): any => {
if (!values || typeof values !== 'object') {
return values
}

const processed = { ...values }

const processObject = (obj: any): any => {
if (typeof obj === 'string') {
// Check if this string should be multiline
if (obj.includes('\n') || obj.length > 80) {
return obj
}
return obj
}

if (Array.isArray(obj)) {
return obj.map(processObject)
}

if (obj && typeof obj === 'object') {
const result: any = {}
for (const [key, value] of Object.entries(obj)) {
result[key] = processObject(value)
}
return result
}

return obj
}

return processObject(processed)
}

/**
* Detects if a string value should be treated as multiline
* @param value - The string value to check
* @returns true if the value should be multiline
*/
export const shouldBeMultiline = (value: string): boolean => {
if (!value || typeof value !== 'string') {
return false
}

// Check for newlines
if (value.includes('\n')) {
return true
}

// Check for long strings
if (value.length > 80) {
return true
}

// Check for multiline indicators
const multilineIndicators = [
'#cloud-config',
'#!/',
'---',
'```',
'BEGIN',
'END',
'-----BEGIN',
'-----END',
]

return multilineIndicators.some(indicator => value.includes(indicator))
}
4 changes: 3 additions & 1 deletion src/endpoints/forms/formSync/utils/onValuesChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { OpenAPIV2 } from 'openapi-types'
import { TFormName } from 'src/localTypes/forms'
import { removeEmptyFormValues, renameBrokenFieldBack } from './removeAndRename'
import { normalizeValuesForQuotas } from './normalizeQuotas'
import { processMultilineInFormValues } from './multilineHandler'

export const onValuesChange = ({
values,
Expand All @@ -15,5 +16,6 @@ export const onValuesChange = ({
const cleanSchema = removeEmptyFormValues(values, persistedKeys)
const fixedCleanSchema = renameBrokenFieldBack(cleanSchema)
const quotasFixedSchema = normalizeValuesForQuotas(fixedCleanSchema, properties)
return quotasFixedSchema
const multilineProcessedSchema = processMultilineInFormValues(quotasFixedSchema)
return multilineProcessedSchema
}
1 change: 1 addition & 0 deletions src/endpoints/forms/formSync/utils/onYamlChange.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OpenAPIV2 } from 'openapi-types'
import { renameBrokenFieldBackToFormAgain } from './removeAndRename'
import { normalizeValuesForQuotasToNumber } from './normalizeQuotas'
import { processMultilineInYaml } from './multilineHandler'

export const onYamlChange = ({
values,
Expand Down