Skip to content

Commit e64d3ce

Browse files
authored
Merge pull request #7 from placemark/json-errors
Handle JSON errors
2 parents 9e6363b + 6f408ab commit e64d3ce

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

src/errors.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ interface HintIssueType extends HintIssueBase {
1414
code: 'invalid_type';
1515
}
1616

17-
export type HintIssue = HintIssueRootType | HintIssueType;
17+
interface HintJSONIssue {
18+
code: 'invalid_json';
19+
line: number;
20+
}
21+
22+
export type HintIssue = HintIssueRootType | HintIssueType | HintJSONIssue;
1823

1924
export class HintError extends Error {
2025
issues: HintIssue[] = [];

src/index.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,8 @@ export const check = (jsonStr: string): GeoJSON => {
168168
});
169169
} catch (e) {
170170
issues.push({
171-
code: 'invalid_type',
172-
loc: {
173-
start: {
174-
line: e.line,
175-
column: e.column,
176-
offset: 0,
177-
},
178-
end: {
179-
line: e.line,
180-
column: e.column,
181-
offset: 0,
182-
},
183-
},
171+
code: 'invalid_json',
172+
line: e.line,
184173
});
185174
}
186175
if (ast) checkObject(issues, ast.body);

test/bad.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ describe('check', () => {
4040
).toThrow(HintError);
4141
});
4242

43+
it('bad json', () => {
44+
try {
45+
check(
46+
`{
47+
"type": "MultiPoint"
48+
"coordinates": [["foo", "bar"]]
49+
}`
50+
);
51+
} catch (e) {
52+
expect(e.issues[0]).toEqual({
53+
code: 'invalid_json',
54+
line: 3,
55+
});
56+
}
57+
});
58+
4359
describe('works with fixtures', () => {
4460
const fixtureNames = readdirSync(Path.join(__dirname, './fixture/bad/'));
4561
for (let name of fixtureNames) {

0 commit comments

Comments
 (0)