Skip to content

Instantly share code, notes, and snippets.

@olaferlandsen
Last active May 7, 2018 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olaferlandsen/4e1a8aaaab0691c63a0e4cff5e9a8780 to your computer and use it in GitHub Desktop.
Save olaferlandsen/4e1a8aaaab0691c63a0e4cff5e9a8780 to your computer and use it in GitHub Desktop.
Create, validate and use "Object Schemas"

API USAGE

Methods

object ObjectSchema (object value, object options[, boolean throwError])

Params

value

This param only accept object as data type and this is a original value to evaluate.

options

this param need a object with rules, and this use the next schema:

{
    PROPERTY : OPTIONS
}

throwError

This param is optional, and only accept boolean. If you set it as true then this function force throw new Error and you will need use try {} catch {}.

Availables options
  • [string] type
  • [boolean] nullable
  • [any] defaultValue

####### Available data types

  • Only array: array or []
  • Array, null or object: object
  • PLain object: {}, plainObject
  • integer : integer
  • float: float
  • number(float, integer): numeric, number
  • string : string

Examples

ObjectSchema({name : "John Smith", age : 22, data : {}}, {
	name : {type : "string"},
	age : {type: "number", nullable : true, defaultValue : 1},
	data : { type :"plainObject"}
}, true); // return {name : "john Smith", age : 22, data : {}}

ObjectSchema({name : "john Smith", age : 22, data : {}}, {
	name : {type : "string"},
	age : {type: "float", nullable : true, defaultValue : 1},
	data : { type :"plainObject"}
}, true); // given error

ObjectSchema({name : "john Smith", age : 22, data : {}}, {
	name : {type : "string"},
	age : {type: "float", nullable : true, defaultValue : 1},
	data : { type :"plainObject"}
}); // return {name : "john Smith", data : {}}

ObjectSchema({name : "john Smith", age : 22, data : []}, {
	name : {type : "string"},
	age : {type: "float", nullable : true, defaultValue : 1},
	data : { type :"object"}
}); // return {name : "john Smith", data : []}


ObjectSchema({name : "john Smith", age : 22, data : []}, {
	name : {type : "string"},
	age : {type: "float", nullable : true, defaultValue : 1},
	data : { type :"array"}
}); // return {name : "john Smith", data : []}
window.ObjectSchema = function (value, options, throwError) {
var regexpArray = /(array|\[([\]]+)\])/i;
var finalObject = {};
if (typeof options !== "object" || options === null) return false;
if (typeof value !== "object" || value === null) return false;
var defaultOptions = {type : "string", defaultValue : null, nullable : false};
for (var property in value) {
var v = value[property], o = options[property];
if (property in options) {
o = Object.assign(defaultOptions, o || {});
if (value[property] === null) {
if (o.nullable === true) finalObject[property] = o.defaultValue;
else {
if (throwError === true) throw new Error(`Can't support property ${property} as null or undefined`);
delete value[ property ];
}
}
else if (Array.isArray(v) && regexpArray.test(o.type)) finalObject[property] = v;
else if ((o.type === "plainObject" || o.type === "{}") && !Array.isArray(v)) finalObject[property] = v;
else if (o.type === "numeric" && typeof v === "number") finalObject[property] = v;
else if (o.type === "integer" && Number.isInteger(v)) finalObject[property] = v;
else if (o.type === "float" && Number(v) === v && v % 1 !== 0) finalObject[property] = v;
else if (typeof v === o.type) finalObject[property] = v;
else {
if (throwError === true) throw new Error(`Can't support property ${property} as ${typeof v} because you need it as ${o.type}`);
delete value[ property ];
}
}
else delete value[ property ];
}
return finalObject;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment