mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-12-12 00:31:11 -05:00
47 lines
1.5 KiB
React
47 lines
1.5 KiB
React
|
var defaults = require('lodash.defaultsdeep');
|
||
|
var libphonenumber = require('google-libphonenumber');
|
||
|
var phoneNumberUtil = libphonenumber.PhoneNumberUtil.getInstance();
|
||
|
var React = require('react');
|
||
|
|
||
|
module.exports = {};
|
||
|
|
||
|
module.exports.validations = {
|
||
|
notEquals: function (values, value, neq) {
|
||
|
return value !== neq;
|
||
|
},
|
||
|
notEqualsField: function (values, value, field) {
|
||
|
return value !== values[field];
|
||
|
},
|
||
|
isPhone: function (values, value) {
|
||
|
if (typeof value === 'undefined') return true;
|
||
|
if (value && value.national_number === '+') return true;
|
||
|
try {
|
||
|
var parsed = phoneNumberUtil.parse(value.national_number, value.country_code.iso2);
|
||
|
} catch (err) {
|
||
|
return false;
|
||
|
}
|
||
|
return phoneNumberUtil.isValidNumber(parsed);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
module.exports.validationHOCFactory = function (defaultValidationErrors) {
|
||
|
return function (Component) {
|
||
|
var ValidatedComponent = React.createClass({
|
||
|
render: function () {
|
||
|
var validationErrors = defaults(
|
||
|
defaultValidationErrors,
|
||
|
this.props.validationErrors
|
||
|
);
|
||
|
return (
|
||
|
<Component {...this.props} validationErrors={validationErrors} />
|
||
|
);
|
||
|
}
|
||
|
});
|
||
|
return ValidatedComponent;
|
||
|
};
|
||
|
};
|
||
|
|
||
|
module.exports.defaultValidationHOC = module.exports.validationHOCFactory({
|
||
|
isDefaultRequiredValue: 'This field is required'
|
||
|
});
|