Skip to content

Instantly share code, notes, and snippets.

@sgromkov
Last active February 5, 2023 22:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgromkov/bd65afb17cb26f70def920a33ace2ada to your computer and use it in GitHub Desktop.
Save sgromkov/bd65afb17cb26f70def920a33ace2ada to your computer and use it in GitHub Desktop.
Validate JSON String. Covered by unit tests on Jest
/**
* Checks the argument for compliance with valid json
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch#The_exception_identifier
* @param {*} text
* @returns {boolean}
*/
export function isValidJson(text) {
try {
JSON.parse(text);
return true;
} catch {
return false;
}
}
/**
* Checks the argument for compliance with valid JSONObject
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON#Full_JSON_syntax
* @param {*} text
* @returns {boolean}
*/
export function isValidJsonObject(text) {
if (typeof text !== 'string') {
return false;
}
const startsWithOpeningCurlyBrace = text.indexOf('{') === 0;
const endsWithClosingCurlyBrace = text.lastIndexOf('}') === (text.length - 1);
if (startsWithOpeningCurlyBrace && endsWithClosingCurlyBrace) {
return isValidJson(text);
}
return false;
}
import { isValidJson, isValidJsonObject } from './jsonValidator';
describe('isValidJson()', () => {
it('should return false if the argument could not be parsed as json', () => {
expect(isValidJson()).toBe(false);
expect(isValidJson(undefined)).toBe(false);
expect(isValidJson(NaN)).toBe(false);
expect(isValidJson(new Error('error instance'))).toBe(false);
expect(isValidJson('')).toBe(false);
expect(isValidJson('simple string')).toBe(false);
expect(isValidJson('.01')).toBe(false);
expect(isValidJson([])).toBe(false);
expect(isValidJson(['filled', 'array'])).toBe(false);
expect(isValidJson('[\'filled\',\'array\']')).toBe(false);
expect(isValidJson({})).toBe(false);
expect(isValidJson({filled: 'object'})).toBe(false);
expect(isValidJson('{\'filled\':\'object\'}')).toBe(false);
expect(isValidJson('{"filled":"object",}')).toBe(false);
});
it('should return true if the argument could be parsed as json', () => {
expect(isValidJson(null)).toBe(true);
expect(isValidJson('null')).toBe(true);
expect(isValidJson(true)).toBe(true);
expect(isValidJson('true')).toBe(true);
expect(isValidJson(false)).toBe(true);
expect(isValidJson('false')).toBe(true);
expect(isValidJson(10)).toBe(true);
expect(isValidJson('10')).toBe(true);
expect(isValidJson(-10)).toBe(true);
expect(isValidJson('-10')).toBe(true);
expect(isValidJson(10.01)).toBe(true);
expect(isValidJson('10.01')).toBe(true);
expect(isValidJson(-10.01)).toBe(true);
expect(isValidJson('-10.01')).toBe(true);
expect(isValidJson(0.01)).toBe(true);
expect(isValidJson('0.01')).toBe(true);
expect(isValidJson(-0.01)).toBe(true);
expect(isValidJson('-0.01')).toBe(true);
expect(isValidJson(.01)).toBe(true);
expect(isValidJson(0)).toBe(true);
expect(isValidJson('0')).toBe(true);
expect(isValidJson('{}')).toBe(true);
expect(isValidJson('{"filled":"object"}')).toBe(true);
expect(isValidJson('{"filled":"object","with":"two keys"}')).toBe(true);
expect(isValidJson("{\"filled\":\"object\"}")).toBe(true);
expect(isValidJson('[]')).toBe(true);
expect(isValidJson('["filled","array"]')).toBe(true);
expect(isValidJson("[\"filled\",\"array\"]")).toBe(true);
});
});
describe('isValidJsonObject()', () => {
it('should return false if the argument could not be parsed as a valid json object', () => {
expect(isValidJsonObject()).toBe(false);
expect(isValidJsonObject(undefined)).toBe(false);
expect(isValidJsonObject(NaN)).toBe(false);
expect(isValidJsonObject(new Error('error instance'))).toBe(false);
expect(isValidJsonObject('')).toBe(false);
expect(isValidJsonObject('simple string')).toBe(false);
expect(isValidJsonObject('.01')).toBe(false);
expect(isValidJsonObject([])).toBe(false);
expect(isValidJsonObject(['filled', 'array'])).toBe(false);
expect(isValidJsonObject('[\'filled\',\'array\']')).toBe(false);
expect(isValidJsonObject({})).toBe(false);
expect(isValidJsonObject({filled: 'object'})).toBe(false);
expect(isValidJsonObject('{\'filled\':\'object\'}')).toBe(false);
expect(isValidJsonObject('{"filled":"object",}')).toBe(false);
expect(isValidJsonObject(null)).toBe(false);
expect(isValidJsonObject('null')).toBe(false);
expect(isValidJsonObject(true)).toBe(false);
expect(isValidJsonObject('true')).toBe(false);
expect(isValidJsonObject(false)).toBe(false);
expect(isValidJsonObject('false')).toBe(false);
expect(isValidJsonObject(10)).toBe(false);
expect(isValidJsonObject('10')).toBe(false);
expect(isValidJsonObject(-10)).toBe(false);
expect(isValidJsonObject('-10')).toBe(false);
expect(isValidJsonObject(10.01)).toBe(false);
expect(isValidJsonObject('10.01')).toBe(false);
expect(isValidJsonObject(-10.01)).toBe(false);
expect(isValidJsonObject('-10.01')).toBe(false);
expect(isValidJsonObject(0.01)).toBe(false);
expect(isValidJsonObject('0.01')).toBe(false);
expect(isValidJsonObject(-0.01)).toBe(false);
expect(isValidJsonObject('-0.01')).toBe(false);
expect(isValidJsonObject(.01)).toBe(false);
expect(isValidJsonObject(0)).toBe(false);
expect(isValidJsonObject('0')).toBe(false);
expect(isValidJsonObject('[]')).toBe(false);
expect(isValidJsonObject('["filled","array"]')).toBe(false);
expect(isValidJsonObject("[\"filled\",\"array\"]")).toBe(false);
});
it('should return true if the argument could be parsed as a valid json object', () => {
expect(isValidJsonObject('{}')).toBe(true);
expect(isValidJsonObject('{"filled":"object"}')).toBe(true);
expect(isValidJsonObject('{"filled":"object","with":"two keys"}')).toBe(true);
expect(isValidJsonObject("{\"filled\":\"object\"}")).toBe(true);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment