wehelpjs
Version:
wehelpjs is the JavaScript API Library for the WeYouMe blockchain
119 lines (91 loc) • 2.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.validateAccountName = exports.accountNotExist = exports.accountExist = exports.isNotUsername = exports.isUsername = void 0;
var _api = _interopRequireDefault(require("../api"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
require('@babel/polyfill');
const isUsername = async username => {
const accounts = await _api.default.getAccountsAsync([username]);
return !!accounts[0];
};
exports.isUsername = isUsername;
const isNotUsername = async username => !(await isUsername(username));
exports.isNotUsername = isNotUsername;
const accountExist = (rule, value, callback) => {
_api.default.getAccounts([value], (err, result) => {
if (result && result.find(a => a.name === value)) {
callback();
} else {
callback(['Account name is not found']);
}
});
};
exports.accountExist = accountExist;
const accountNotExist = (rule, value, callback) => {
_api.default.getAccounts([value], (err, result) => {
if (result && result.find(a => a.name === value)) {
callback(['Account name is not available']);
} else {
callback();
}
});
}; // https://github.com/WeYouMe/weauth/blob/eaf8a02658b8deaef376ec90b81d0866e52582cc/app/utils/ChainValidation.js#L4
exports.accountNotExist = accountNotExist;
const validateAccountName = (rule, value, callback, intl) => {
let i;
let label;
let len;
let segment = '';
if (!value) {
return callback(intl.formatMessage({
id: 'error_username_required'
}));
}
const length = value.length;
if (length < 3) {
return callback(intl.formatMessage({
id: 'error_validation_account_min'
}));
}
if (length > 16) {
return callback(intl.formatMessage({
id: 'error_validation_account_max'
}));
}
if (/\./.test(value)) {
segment = '_segment';
}
const ref = value.split('.');
for (i = 0, len = ref.length; i < len; i += 1) {
label = ref[i];
if (!/^[a-z]/.test(label)) {
return callback(intl.formatMessage({
id: `error_validation_account${segment}_start`
}));
}
if (!/^[a-z0-9-]*$/.test(label)) {
return callback(intl.formatMessage({
id: `error_validation_account${segment}_alpha`
}));
}
if (/--/.test(label)) {
return callback(intl.formatMessage({
id: `error_validation_account${segment}_dash`
}));
}
if (!/[a-z0-9]$/.test(label)) {
return callback(intl.formatMessage({
id: `error_validation_account${segment}_end`
}));
}
if (!(label.length >= 3)) {
return callback(intl.formatMessage({
id: `error_validation_account${segment}_min`
}));
}
}
return callback();
};
exports.validateAccountName = validateAccountName;