55 lines
1.0 KiB
JavaScript
55 lines
1.0 KiB
JavaScript
const {
|
|
isString,
|
|
isDefinedOrThrow,
|
|
isStringOrObjectOrThrow
|
|
} = require("../../utils/fun.js");
|
|
|
|
function JSONParser() {
|
|
|
|
}
|
|
|
|
const invalidPayloadTypeError =
|
|
new Error("invalid payload type, allowed are: string or object");
|
|
|
|
const nullOrIndefinedPayload =
|
|
new Error("null or undefined payload");
|
|
|
|
// Function
|
|
const asJSON = (v) => (isString(v) ? JSON.parse(v) : v);
|
|
|
|
/**
|
|
* Level 0 of validation: is that string? is that JSON?
|
|
*/
|
|
function validateAndParse(payload) {
|
|
|
|
var json =
|
|
Array.of(payload)
|
|
.filter((p) => isDefinedOrThrow(p, nullOrIndefinedPayload))
|
|
.filter((p) => isStringOrObjectOrThrow(p, invalidPayloadTypeError))
|
|
.map(asJSON)
|
|
.shift();
|
|
|
|
return json;
|
|
}
|
|
|
|
/*
|
|
* Level 1 of validation: is that follow a spec?
|
|
*/
|
|
function validateSpec(payload, spec) {
|
|
|
|
// is that follow the spec?
|
|
spec.check(payload);
|
|
|
|
return payload;
|
|
}
|
|
|
|
JSONParser.prototype.parse = function(payload) {
|
|
|
|
//is that string? is that JSON?
|
|
var valid = validateAndParse(payload);
|
|
|
|
return valid;
|
|
};
|
|
|
|
module.exports = JSONParser;
|