From 3551a8fda18f69183b5e89bdd0305c77f11eeb47 Mon Sep 17 00:00:00 2001 From: Bernard Date: Fri, 7 Apr 2017 23:18:38 +0200 Subject: [PATCH 1/3] Added iface propType --- addon/utils/prop-types.js | 30 +++++++++++++++++++ addon/utils/validators/iface.js | 52 +++++++++++++++++++++++++++++++++ addon/utils/validators/index.js | 2 ++ 3 files changed, 84 insertions(+) create mode 100644 addon/utils/validators/iface.js diff --git a/addon/utils/prop-types.js b/addon/utils/prop-types.js index 5434be7..4539939 100644 --- a/addon/utils/prop-types.js +++ b/addon/utils/prop-types.js @@ -209,6 +209,36 @@ PropTypes.shape = function (typeDefs, options) { return type } +PropTypes.iface = function (typeDefs, options) { + const type = generateType('iface') + + if (typeOf(typeDefs) === 'object') { + Object.keys(typeDefs) + .forEach((key) => { + typeDefs[key] = getDef(typeDefs[key]) + }) + } + + type.typeDefs = typeDefs + + if (typeOf(options) !== 'object') { + type.isRequired.typeDefs = type.typeDefs + return type + } + + delete type.isRequired + + if ('required' in options) { + type.required = options.required + } + + if (options.updatable === false) { + type.updatable = false + } + + return type +} + export default PropTypes export {validators} export {logger} diff --git a/addon/utils/validators/iface.js b/addon/utils/validators/iface.js new file mode 100644 index 0000000..e05c2a6 --- /dev/null +++ b/addon/utils/validators/iface.js @@ -0,0 +1,52 @@ +/** + * The PropTypes.iface validator + */ + +import Ember from 'ember' +const {get, typeOf} = Ember + +import logger from '../logger' + +export default function (validators, ctx, name, value, def, logErrors, throwErrors) { + const typeDefs = def.typeDefs + let msg = `Expected property ${name} to match given interface` + let iface + try { + iface = JSON.stringify(value, null, ' ') + msg = `${msg}, but instead got value ${iface}` + } catch (e) {} + + if (typeOf(typeDefs) !== 'object') { + logger.warn(ctx, 'PropTypes.iface() requires a plain object to be be passed in as an argument', throwErrors) + return false + } + + if (typeOf(value) !== 'object') { + logger.warn(ctx, msg, throwErrors) + return false + } + + let valid = Object.keys(typeDefs).every((key) => { + const typeDef = typeDefs[key] + + const objectValue = get(value, key) + if (objectValue === undefined) { + if (!typeDef.required) { + return true + } else { + if (logErrors) { + logger.warn(ctx, `Property ${name} is missing required property ${key}`, throwErrors) + } + return false + } + } + + return validators[typeDef.type](ctx, `${name}.${key}`, objectValue, typeDef, logErrors, throwErrors) + }) + + if (!valid && logErrors) { + logger.warn(ctx, msg, throwErrors) + } + + return valid +} diff --git a/addon/utils/validators/index.js b/addon/utils/validators/index.js index c03cbc6..31ec049 100644 --- a/addon/utils/validators/index.js +++ b/addon/utils/validators/index.js @@ -9,6 +9,7 @@ import element from './element' import emberComponent from './ember-component' import emberObject from './ember-object' import func from './func' +import iface from './iface' import instanceOf from './instance-of' import nullFn from './null' import number from './number' @@ -43,6 +44,7 @@ const validators = { objectAssign(validators, { arrayOf: arrayOf.bind(this, validators), + iface: iface.bind(this, validators), oneOfType: oneOfType.bind(this, validators), shape: shape.bind(this, validators) }) From 4afd0aaa1ce0151efb7ec81157f0c5b8aeee4663 Mon Sep 17 00:00:00 2001 From: gustaff-weldon Date: Mon, 23 Oct 2017 15:28:21 +0200 Subject: [PATCH 2/3] Allowed Ember object instances in iface --- addon/utils/validators/iface.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/addon/utils/validators/iface.js b/addon/utils/validators/iface.js index e05c2a6..09a1780 100644 --- a/addon/utils/validators/iface.js +++ b/addon/utils/validators/iface.js @@ -21,7 +21,8 @@ export default function (validators, ctx, name, value, def, logErrors, throwErro return false } - if (typeOf(value) !== 'object') { + // allow POJOs as well as Ember Object instances for greater flexibility + if (typeOf(value) !== 'object' && typeOf(value) !== 'instance') { logger.warn(ctx, msg, throwErrors) return false } From 50471f32d94427d728daf6a885e3d618f026c814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Prokop?= Date: Mon, 3 Dec 2018 17:38:57 +0100 Subject: [PATCH 3/3] Add eslint comment, the same which is in shape --- addon/utils/validators/iface.js | 1 + 1 file changed, 1 insertion(+) diff --git a/addon/utils/validators/iface.js b/addon/utils/validators/iface.js index 09a1780..dd84da4 100644 --- a/addon/utils/validators/iface.js +++ b/addon/utils/validators/iface.js @@ -7,6 +7,7 @@ const {get, typeOf} = Ember import logger from '../logger' +/* eslint-disable complexity */ export default function (validators, ctx, name, value, def, logErrors, throwErrors) { const typeDefs = def.typeDefs let msg = `Expected property ${name} to match given interface`