4.0.0 • Published 1 year ago
sanitize-json v4.0.0
Sanitize Json
inspired from covid-19
Package is created for singuar purpose, to remove extra properties from a json object. This is most help full if you want to filter incoming requests.
- filter out json
- verify if property matches given conditions
- create custom sanitizer functions:
Github
Usage
offten there is a sicutation where you get a dirty json data:
// obj from untrusted source
const dirtyJsonObject = {
name: 'Panth',
email: 'email@abc.df',
age: 20,
username: 'very_long_username',
isAdmin: true,
};
here isAdmin property should not be.
then create a sanitizerđź’•:
const profileSanitizer = isStructOf({
name: hasValOf.string,
email: checkIfIt(hasValOf.string, isEmail),
age: checkIfIt(hasValOf.number, isInteger, shouldBe('>=', 18)),
username: checkIfIt(
hasValOf.string,
is(username => username.length > 3 && username.length < 30)
),
});
then use sanitizer on dirty Json Objects
const cleanJsonObj = sanitizeJson(profileSanitizer, dirtyJsonObject);
console.log(cleanJsonObj.val);
// { name: 'Panth', email: 'email@abc.df', age: 20, username: 'very_long_username' }
you can create basic nested interface sanitizers:
const someSanitizer = isStructOf({
a: isStructOf({
b: hasValOf.number,
a: hasValOf.boolean,
}),
});
you can also have logic opraters "and" or "or":
const someSanitizer = isStructOf({
a: either(hasValOf.number, hasValOf.boolean),
});
console.log(sanitizeJson(someSanitizer, { a: true }));
// { err: false, val: {a: true} }
console.log(sanitizeJson(someSanitizer, { a: 5 }));
// { err: false, val: {a: 5} }
console.log(sanitizeJson(someSanitizer, { a: 'sa' }));
// { err: true, val: #Error }
Consept (create your own sanitizor)
interface data {}
function dataSanitizor(val: data): data {
if (data_in_val_is_valid) return create_copy_of(val);
throw 'reason, what was wrong with ${val}';
}
APIs: (prebuilt sanitizor)
isStructOf
interface a {
p1: string;
}
interface b {
a: a;
}
const a = isStructOf({ p1: hasValOf.string });
const b = isStructOf({ a: a });
either
type a = string;
interface b {}
type c = a | b;
const a = hasValOf.string;
const b = isStructOf({});
const c = either(a, b);
combine
interface a {}
interface b {}
type c = a & b;
const a = isStructOf({});
const b = isStructOf({});
const c = combine(a, b);
isListOf
interface a {}
type b = a[]; // List<a>;
//
const a = isStructOf({});
const b = isListOf(a);
isMapOf
interface a {}
type b = { [key: string]: a };
const a = isStructOf({});
const b = isMapOf(a);
checkIfIt
interface profile {
...
age: number; // number must be int, and (18 or 18+)
}
const profileSanitizer = isStructOf({
...,
age: checkIfIt(hasValOf.number, isInteger, shouldBe(">=", 18))
})
is
interface profile {
...
username: string; // string must pass function validUsername
}
function validUsername(username: string): boolean {
...
}
const profileSanitizer = isStructOf({
...,
username: checkIfIt(hasValOf.string, is(validUsername))
})