Files
DiceCloud/app/imports/api/properties/subSchemas/inlineCalculationField.js

75 lines
2.0 KiB
JavaScript

import SimpleSchema from 'simpl-schema';
import ErrorSchema from '/imports/api/properties/subSchemas/ErrorSchema.js';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
// Get schemas that apply fields directly so they can be gracefully extended
// because {type: Schema} fields can't be extended
function inlineCalculationFieldToCompute(field){
return new SimpleSchema({
// The object should already be set, but set again just in case
[field]: {
type: Object,
optional: true,
},
[`${field}.text`]: {
type: String,
optional: true,
max: STORAGE_LIMITS.inlineCalculationField,
},
});
}
function computedOnlyInlineCalculationField(field){
return new SimpleSchema({
// The object should already be set, but set again just in case
[field]: {
type: Object,
optional: true,
},
[`${field}.value`]: {
type: String,
optional: true,
max: STORAGE_LIMITS.inlineCalculationField,
},
[`${field}.inlineCalculations`]: {
type: Array,
defaultValue: [],
maxCount: STORAGE_LIMITS.inlineCalculationCount,
},
[`${field}.inlineCalculations.$`]: {
type: Object,
parseLevel: 'compile',
},
// The part between bracers {}
[`${field}.inlineCalculations.$.calculation`]: {
type: String,
max: STORAGE_LIMITS.calculation,
},
[`${field}.inlineCalculations.$.value`]: {
type: SimpleSchema.oneOf(String, Number),
optional: true,
max: STORAGE_LIMITS.calculation,
},
[`${field}.inlineCalculations.$.errors`]: {
type: Array,
optional: true,
maxCount: STORAGE_LIMITS.errorCount,
},
[`${field}.inlineCalculations.$.errors.$`]: {
type: ErrorSchema,
},
});
}
function computedInlineCalculationField(field){
return inlineCalculationFieldToCompute(field).extend(
computedOnlyInlineCalculationField(field)
)
}
export {
inlineCalculationFieldToCompute,
computedOnlyInlineCalculationField,
computedInlineCalculationField,
};