Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for iface type #180

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions addon/utils/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
54 changes: 54 additions & 0 deletions addon/utils/validators/iface.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* The PropTypes.iface validator
*/

import Ember from 'ember'
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`
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
}

// 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
}

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
}
2 changes: 2 additions & 0 deletions addon/utils/validators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
})
Expand Down