UNPKG

feature-u

Version:

Feature Based Project Organization for React

93 lines (78 loc) 2.5 kB
'use strict'; exports.__esModule = true; var _lodash = require('lodash.isstring'); var _lodash2 = _interopRequireDefault(_lodash); var _lodash3 = require('lodash.isfunction'); var _lodash4 = _interopRequireDefault(_lodash3); var _isComponent = require('../util/isComponent'); var _isComponent2 = _interopRequireDefault(_isComponent); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * @typedef {Object} fassetValidations * * A pre-defined container of fasset validation functions, which can * be employed in the {{book.api.fassetsAspect}} `use` directive. * This allows the `use` directive to specify data type and content * validation constraints. * * These validations are available as a convenience. Additional * validations can be created as needed. * * The validation API should adhere to the following signature: * * ``` * + fassetValidationFn(fassetsValue): string || null * ``` * * A return value of null represents a valid value, while a string * specifies a validation error that feature-u will format as follows * (see ${returnStr}): * * ``` * VALIDATION ERROR in resource: '${fassetsKey}', * expecting: ${returnStr} ... * resource defined in Feature: '${resource.definingFeature}', * usage contract '${useKey}' found in Feature: '${featureName}' * ``` * * The following pre-defined validations are promoted through `fassetValidations`: * - `any`: any type (except undefined) * - `comp`: a react component * - `fn`: a function * - `str`: a string * - `bool`: a boolean * * **Example**: * ```js * createFeature({ * fassets: { * use: [ * 'MainPage.*.link', // DEFAULT: required of type any * ['MainPage.*.body', {required: false, type: fassetValidations.comp}], * ], * }, * }); * ``` */ exports.default = { any: any, comp: comp, fn: fn, str: str, bool: bool }; function any(fassetsValue) { return fassetsValue !== undefined ? null : 'anthing but: undefined'; } function comp(fassetsValue) { return (0, _isComponent2.default)(fassetsValue) ? null : 'React Component'; } function fn(fassetsValue) { return (0, _lodash4.default)(fassetsValue) ? null : 'function'; } function str(fassetsValue) { return (0, _lodash2.default)(fassetsValue) ? null : 'string'; } function bool(fassetsValue) { return fassetsValue === true || fassetsValue === false ? null : 'boolean'; }