mirror of
https://github.com/strapi/strapi.git
synced 2025-09-25 16:29:34 +00:00
move utility functions to their own file
This commit is contained in:
parent
ce534cf568
commit
4d3d4c8d55
@ -17,6 +17,8 @@ const { models: utilsModels } = require('strapi-utils');
|
||||
|
||||
// Local helpers.
|
||||
const utils = require('./utils/');
|
||||
const _utils = utils();
|
||||
|
||||
const relations = require('./relations');
|
||||
|
||||
/**
|
||||
@ -524,23 +526,11 @@ module.exports = function (strapi) {
|
||||
|
||||
postProcessValue: (value) => {
|
||||
if (_.isArray(value)) {
|
||||
return value.map(valueToId);
|
||||
return value.map(_utils.valueToId);
|
||||
}
|
||||
return valueToId(value);
|
||||
return _utils.valueToId(value);
|
||||
}
|
||||
}, relations);
|
||||
|
||||
return hook;
|
||||
};
|
||||
|
||||
const valueToId = value => {
|
||||
return isMongoId(value)
|
||||
? mongoose.Types.ObjectId(value)
|
||||
: value;
|
||||
};
|
||||
|
||||
const isMongoId = (value) => {
|
||||
const hexadecimal = /^[0-9A-F]+$/i;
|
||||
|
||||
return hexadecimal.test(value) && value.length === 24;
|
||||
};
|
||||
|
@ -46,7 +46,16 @@ module.exports = (mongoose = new Mongoose()) => {
|
||||
default:
|
||||
}
|
||||
},
|
||||
isObjectId: v => mongoose.Types.ObjectId.isValid(v),
|
||||
toObjectId: v => mongoose.Types.ObjectId(v),
|
||||
valueToId: value => {
|
||||
return this.isMongoId(value)
|
||||
? mongoose.Types.ObjectId(value)
|
||||
: value;
|
||||
},
|
||||
isMongoId: (value) => {
|
||||
// Here we don't use mongoose.Types.ObjectId.isValid method because it's a weird check,
|
||||
// it returns for instance true for any integer value ¯\_(ツ)_/¯
|
||||
const hexadecimal = /^[0-9A-F]+$/i;
|
||||
return hexadecimal.test(value) && value.length === 24;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user