feat: Recovery option for jsonParse helper (#10182)

Co-authored-by: Elias Meire <elias@meire.dev>
This commit is contained in:
Michael Kret
2024-07-26 20:05:25 +03:00
committed by GitHub
parent 1718125c6d
commit d165b33cea
3 changed files with 75 additions and 6 deletions

View File

@@ -145,10 +145,14 @@ describe('Type Validation', () => {
);
});
it('should validate and cast JSON properly', () => {
it('should validate and cast JSON & JS objects properly', () => {
const VALID_OBJECTS = [
['{"a": 1}', { a: 1 }],
['{a: 1}', { a: 1 }],
["{'a': '1'}", { a: '1' }],
["{'\\'single quoted\\' \"double quoted\"': 1}", { '\'single quoted\' "double quoted"': 1 }],
['{"a": 1, "b": { "c": 10, "d": "test"}}', { a: 1, b: { c: 10, d: 'test' } }],
["{\"a\": 1, b: { 'c': 10, d: 'test'}}", { a: 1, b: { c: 10, d: 'test' } }],
[{ name: 'John' }, { name: 'John' }],
[
{ name: 'John', address: { street: 'Via Roma', city: 'Milano' } },
@@ -162,19 +166,18 @@ describe('Type Validation', () => {
}),
);
const INVALID_JSON = [
const INVALID_OBJECTS = [
['one', 'two'],
'1',
'[1]',
'1.1',
1.1,
'"a"',
'{a: 1}',
'["apples", "oranges"]',
[{ name: 'john' }, { name: 'bob' }],
'[ { name: "john" }, { name: "bob" } ]',
];
INVALID_JSON.forEach((value) =>
INVALID_OBJECTS.forEach((value) =>
expect(validateFieldType('json', value, 'object').valid).toEqual(false),
);
});