Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto convert non-string content value to JSON string under text mode #166

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 3 additions & 3 deletions src/lib/components/JSONEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
let open // svelte-simple-modal context open(...)

$: {
const contentError = validateContentType(content)
const contentError = validateContentType(content, parser, indentation)
if (contentError) {
console.error('Error: ' + contentError)
}
Expand All @@ -121,7 +121,7 @@
export function set(newContent: Content) {
debug('set')

const contentError = validateContentType(newContent)
const contentError = validateContentType(newContent, parser, indentation)
if (contentError) {
throw new Error(contentError)
}
Expand All @@ -135,7 +135,7 @@
export function update(updatedContent: Content) {
debug('update')

const contentError = validateContentType(updatedContent)
const contentError = validateContentType(updatedContent, parser, indentation)
if (contentError) {
throw new Error(contentError)
}
Expand Down
18 changes: 10 additions & 8 deletions src/lib/utils/jsonUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,26 +97,28 @@ describe('jsonUtils', () => {
})

it('should validate content type', () => {
deepStrictEqual(validateContentType({ json: [1, 2, 3] }), null)
deepStrictEqual(validateContentType({ text: '[1,2,3]' }), null)
deepStrictEqual(validateContentType({ json: [1, 2, 3] }, JSON, 2), null)
deepStrictEqual(validateContentType({ text: '[1,2,3]' }, JSON, 2), null)

const jsonTextContent = { text: [1, 2, 3] }, jsonTextContentCopy = { ...jsonTextContent }
validateContentType(jsonTextContent, JSON, 2)
deepStrictEqual(
validateContentType({ text: [1, 2, 3] }),
'Content "text" property must be string'
jsonTextContent.text,
JSON.stringify(jsonTextContentCopy.text, null, 2)
)

deepStrictEqual(
validateContentType({ json: [1, 2, 3], text: '[1,2,3]' }),
validateContentType({ json: [1, 2, 3], text: '[1,2,3]' }, JSON, 2),
'Content must contain either a property "json" or a property "text" but not both'
)

deepStrictEqual(
validateContentType({}),
validateContentType({}, JSON, 2),
'Content must contain either a property "json" or a property "text"'
)

deepStrictEqual(validateContentType([]), 'Content must be an object')
deepStrictEqual(validateContentType(2), 'Content must be an object')
deepStrictEqual(validateContentType([], JSON, 2), 'Content must be an object')
deepStrictEqual(validateContentType(2, JSON, 2), 'Content must be an object')
})

describe('estimateSerializedSize', () => {
Expand Down
10 changes: 8 additions & 2 deletions src/lib/utils/jsonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,11 @@ export function convertValue(
* Returns a string with validation error message when there is an issue,
* or null otherwise
*/
export function validateContentType(content: unknown): string | null {
export function validateContentType(
content: unknown,
parser: JSONParser,
indentation: number | string | null,
): string | null {
if (!isObject(content)) {
return 'Content must be an object'
}
Expand All @@ -307,7 +311,9 @@ export function validateContentType(content: unknown): string | null {
if (content.text === undefined) {
return 'Content must contain either a property "json" or a property "text"'
} else if (typeof content.text !== 'string') {
return 'Content "text" property must be string'
// return 'Content "text" property must be string'
content.text = parser.stringify(content.text, null, indentation)
return null
} else {
return null
}
Expand Down