Changeset View
Changeset View
Standalone View
Standalone View
lib/app/base-chips/field-types/text.js
"use strict"; | "use strict"; | ||||
const Promise = require("bluebird"); | const Promise = require("bluebird"); | ||||
const escape = require("escape-html"); | const escape = require("escape-html"); | ||||
module.exports = { | module.exports = { | ||||
name: "text", | name: "text", | ||||
has_index: function(params){ | has_index: function(params) { | ||||
if(params.full_text_search || params.include_in_search){ | if (params.full_text_search || params.include_in_search) { | ||||
return {original: "text"}; | return { original: "text" }; | ||||
}else{ | } else { | ||||
return false; | return false; | ||||
} | } | ||||
}, | }, | ||||
get_description: function(context, params){ | get_description: function(context, params) { | ||||
return `Text with maximum length ${params.max_length}`; | return `Text with maximum length ${params.max_length}`; | ||||
}, | }, | ||||
is_proper_value: function(context, params, new_value){ | is_proper_value: function(context, params, new_value) { | ||||
const checks = []; | let checks = []; | ||||
checks.push(text => { | |||||
if (typeof new_value !== "string") { | |||||
return Promise.reject( | |||||
`Type of ${new_value} is ${typeof new_value}, not string.` | |||||
); | |||||
} | |||||
}); | |||||
if(params.min_length){ | if (params.min_length) { | ||||
checks.push((text) => { | checks.push(text => { | ||||
if(text.length < params.min_length){ | if (text.length < params.min_length) { | ||||
return Promise.reject(`Text '${new_value}' is too short, minimum length is ${params.min_length} chars.`); | return Promise.reject( | ||||
`Text '${new_value}' is too short, minimum length is ${ | |||||
params.min_length | |||||
} chars.` | |||||
); | |||||
} | } | ||||
}); | }); | ||||
} | } | ||||
if(params.max_length){ | if (params.max_length) { | ||||
checks.push((text) => { | checks.push(text => { | ||||
if(text.length > params.max_length){ | if (text.length > params.max_length) { | ||||
return Promise.reject(`Text '${new_value}' has exceeded max length of ${params.max_length} chars`); | return Promise.reject( | ||||
`Text '${new_value}' has exceeded max length of ${ | |||||
params.max_length | |||||
} chars.` | |||||
); | |||||
} | } | ||||
}); | }); | ||||
} | } | ||||
return Promise.all(checks.map(fn => fn(new_value))) | return Promise.all(checks.map(fn => fn(new_value))).then(() => | ||||
.then(() => Promise.resolve()); | Promise.resolve() | ||||
); | |||||
}, | }, | ||||
encode: function(context, params, value_in_code){ | encode: function(context, params, value_in_code) { | ||||
if (typeof value_in_code === "string" && value_in_code !== null){ | if (typeof value_in_code === "string" && value_in_code !== null) { | ||||
const result = { | const result = { | ||||
"original": value_in_code, | original: value_in_code, | ||||
"safe": escape(value_in_code), | safe: escape(value_in_code), | ||||
valueOf: function(){ | valueOf: function() { | ||||
return value_in_code; | return value_in_code; | ||||
}, | }, | ||||
}; | }; | ||||
return Promise.resolve(result); | return Promise.resolve(result); | ||||
} else { | } else return Promise.resolve(null); | ||||
return Promise.resolve(null); | |||||
} | |||||
}, | }, | ||||
get_aggregation_stages: function(context, params, field_name, query_params){ | get_aggregation_stages: function( | ||||
const filter_value = query_params.filter && query_params.filter[field_name]; | context, | ||||
params, | |||||
field_name, | |||||
query_params | |||||
) { | |||||
const filter_value = | |||||
query_params.filter && query_params.filter[field_name]; | |||||
if(filter_value){ | if (filter_value) { | ||||
if(filter_value.regex){ | if (filter_value.regex) { | ||||
const regex_options = "i"; | const regex_options = "i"; | ||||
const regex_query = {$regex: filter_value.regex, $options: regex_options}; | const regex_query = { | ||||
$regex: filter_value.regex, | |||||
$options: regex_options, | |||||
}; | |||||
return [ | return [ | ||||
{ | { | ||||
$match: { | $match: { | ||||
$or: [ | $or: [ | ||||
{ | { | ||||
[`body.${ field_name }.original`]: regex_query, | [`body.${field_name}.original`]: regex_query, | ||||
}, | }, | ||||
{ | { | ||||
[`body.${ field_name }.safe`]: regex_query, | [`body.${field_name}.safe`]: regex_query, | ||||
} | }, | ||||
] | ], | ||||
}, | }, | ||||
}, | }, | ||||
]; | ]; | ||||
}else{ | } else { | ||||
const value_query = {$eq: filter_value}; | const value_query = { $eq: filter_value }; | ||||
return [ | return [ | ||||
{ | { | ||||
$match: { | $match: { | ||||
$or: [ | $or: [ | ||||
{ | { | ||||
[`body.${ field_name }.original`]: filter_value, | [`body.${field_name}.original`]: filter_value, | ||||
}, | }, | ||||
{ | { | ||||
[`body.${ field_name }.safe`]: filter_value, | [`body.${field_name}.safe`]: filter_value, | ||||
} | }, | ||||
] | ], | ||||
}, | }, | ||||
}, | }, | ||||
]; | ]; | ||||
} | } | ||||
}else{ | } else { | ||||
return []; | return []; | ||||
} | } | ||||
}, | }, | ||||
format: function(context, params, decoded_value, format){ | format: function(context, params, decoded_value, format) { | ||||
if(decoded_value === null || decoded_value === undefined){ | if (decoded_value === null || decoded_value === undefined) { | ||||
return Promise.resolve(decoded_value); | return Promise.resolve(decoded_value); | ||||
} | } | ||||
if(format === undefined){ | if (format === undefined) { | ||||
return decoded_value.safe; | return decoded_value.safe; | ||||
} | } | ||||
return decoded_value[format] ? decoded_value[format] : decoded_value; | return decoded_value[format] ? decoded_value[format] : decoded_value; | ||||
}, | }, | ||||
}; | }; |