Skip to content

Commit

Permalink
Default value and adds required (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBuchholz committed Aug 24, 2018
1 parent 139cda5 commit 354b7b5
Showing 1 changed file with 46 additions and 9 deletions.
55 changes: 46 additions & 9 deletions src/serial/property.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ _gpfErrorDeclare("serial/property", {
*
* Type should be one of the enumeration {@see gpf.serial.types}
*/
"invalidSerialType": "Invalid serial type"
"invalidSerialType": "Invalid serial type",

/**
* ### Summary
*
* Required of serializable property is invalid
*
* ### Description
*
* Required can either be true or false
*/
"invalidSerialRequired": "Invalid serial required"

});

/**
Expand All @@ -44,6 +56,7 @@ gpf.serial = {};
* @typedef gpf.typedef.serializableProperty
* @property {String} name Name of the property
* @property {gpf.serial.types} [type=gpf.serial.types.string] Type of the property
* @property {Boolean} [required=false] Property must have a value
* @see gpf.attributes.Serializable
*/

Expand Down Expand Up @@ -82,33 +95,57 @@ function _gpfSerialPropertyCheckNameRegExp (name) {
}
}

function _gpfSerialPropertyCheckName (name) {
_gpfSerialPropertyCheckNameType(name);
_gpfSerialPropertyCheckNameRegExp(name);
function _gpfSerialPropertyCheckName (property) {
_gpfSerialPropertyCheckNameType(property.name);
_gpfSerialPropertyCheckNameRegExp(property.name);
}

var _gpfSerialPropertyTypes = Object.keys(_GPF_SERIAL_TYPE).map(function (name) {
return _GPF_SERIAL_TYPE[name];
});

function _gpfSerialPropertyCheckType (type) {
function _gpfSerialPropertyCheckTypeExists (type) {
if (_gpfSerialPropertyTypes.indexOf(type) === -1) {
gpf.Error.invalidSerialType();
}
}

function _gpfSerialPropertyCheckType (property) {
if (undefined === property.type) {
property.type = _GPF_SERIAL_TYPE.STRING;
} else {
_gpfSerialPropertyCheckTypeExists(property.type);
}
}

function _gpfSerialPropertyCheckRequiredType (required) {
if (typeof required !== "boolean") {
gpf.Error.invalidSerialRequired();
}
}

function _gpfSerialPropertyCheckRequired (property) {
if (undefined === property.required) {
property.required = false;
} else {
_gpfSerialPropertyCheckRequiredType(property.required);
}
}

/**
* Check that the serializable property definition is valid
*
* @param {gpf.typedef.serializableProperty} property Property definition to validate
* @return {gpf.typedef.serializableProperty} Completed property definition
* @throws {gpf.Error.InvalidSerialName}
* @throws {gpf.Error.InvalidSerialType}
*/
function _gpfSerialPropertyCheck (property) {
_gpfSerialPropertyCheckName(property.name);
if (property.type !== undefined) {
_gpfSerialPropertyCheckType(property.type);
}
property = Object.assign(property); // Clone
_gpfSerialPropertyCheckName(property);
_gpfSerialPropertyCheckType(property);
_gpfSerialPropertyCheckRequired(property);
return property;
}

/*#ifndef(UMD)*/
Expand Down

0 comments on commit 354b7b5

Please sign in to comment.