Created
January 12, 2018 23:53
-
-
Save hellos3b/8a88c2fea7fe83012519ad1ac90e941c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Helpers = { | |
/* Schema keys are defined as either | |
key: Type | |
or | |
key: { type: Type } | |
This helper just converts the shorthand version to key: {type: Type} | |
and converts the Type to a string | |
*/ | |
schema_parse(obj) { | |
if (typeof obj == 'function') { | |
obj = { | |
type: typeof obj() | |
}; | |
} else { | |
obj.type = typeof obj.type(); | |
} | |
return obj; | |
} | |
}; | |
function Schema(schema) { | |
// Definition of types, stored as key: typeof __ | |
const _types = {}; | |
// List | |
const _required = []; | |
// Parse the schema | |
for (let key in schema) { | |
let definition = Helpers.schema_parse(schema[key]); | |
_types[key] = definition.type; | |
if (definition.required) { | |
_required.push(key); | |
} | |
} | |
return function(json) { | |
function _ValidateTypes() { | |
let json_type, | |
target_type, | |
result = true; | |
for (var key in json) { | |
if (_types[key]) { | |
json_type = typeof json[key]; | |
target_type = _types[key]; | |
if (json_type !== target_type) { | |
console.log(`'${key}' is not the right type; Got '${json_type}', was expecting '${target_type}'`); | |
result = false; | |
} | |
} | |
} | |
return result; | |
} | |
function _ValidateRequired() { | |
for (var i = 0; i < _required.length; i++) { | |
const required_key = _required[i]; | |
if (!(required_key in json)) { | |
console.log(required_key + " is required"); | |
return false; | |
} | |
} | |
return true; | |
} | |
return { | |
get valid() { | |
let isValid = _ValidateRequired() && _ValidateTypes(); | |
if (!isValid) { | |
console.warn("Unable to validate JSON", json); | |
} | |
return isValid; | |
}, | |
get json() { | |
return json; | |
} | |
}; | |
}; | |
} | |
/*** | |
Below are tests used just for quick development, ignore this. | |
Just leaving it in the gist to show what the point of the class eventually is | |
*/ | |
const SubscribtionModel = Schema({ | |
name: String, | |
id: Number, | |
email: { | |
type: String, | |
required: true | |
} | |
}); | |
// This should pass, valid JSON | |
const valid1 = { | |
name: "hello", | |
id: 1824, | |
email: "foo@bar.com" | |
}; | |
console.log("##Valid ", SubscribtionModel(valid1).valid); | |
// This should pass, name isn't required | |
const valid2 = { | |
id: 9892, | |
email: "test@gmawil.com" | |
}; | |
console.log("##Valid 2", SubscribtionModel(valid2).valid); | |
// This should fail due to bad types | |
const typeBreak = { | |
name: 1234, | |
id: "hello", | |
email: "foo@bar.com" | |
}; | |
console.log("##Type Break 1", !SubscribtionModel(typeBreak).valid); | |
// This should fail due to bad type | |
const typeBreak2 = { | |
name: "hello", | |
email: 12923 | |
}; | |
console.log("##Type Break 2", !SubscribtionModel(typeBreak2).valid); | |
// This should fail due to required email field | |
const required = { | |
name: "hello" | |
}; | |
console.log("##Required Break", !SubscribtionModel(required).valid); | |
export default Schema; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment