Skip to content

Commit ad11e36

Browse files
committed
just refactoring
1 parent 97d9573 commit ad11e36

File tree

1 file changed

+78
-73
lines changed

1 file changed

+78
-73
lines changed

packages/cubejs-schema-compiler/src/compiler/CubeValidator.ts

Lines changed: 78 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ function formatStatePath(state: Joi.State): string {
4242
return '<unknown path>';
4343
}
4444

45+
function condition(fun, then, otherwise) {
46+
return Joi.alternatives().conditional(
47+
Joi.ref('.'), {
48+
is: Joi.custom((value, helper) => (fun(value) ? value : helper.message({}))),
49+
then,
50+
otherwise
51+
}
52+
);
53+
}
54+
55+
function defined(a) {
56+
return typeof a !== 'undefined';
57+
}
58+
59+
function inherit(a, b) {
60+
return Joi.object().keys({ ...a, ...b });
61+
}
62+
63+
function requireOneOf(...keys) {
64+
return Joi.alternatives().try(
65+
...(keys.map((k) => Joi.object().keys({ [k]: Joi.exist().required() })))
66+
);
67+
}
68+
4569
const regexTimeInterval = Joi.string().custom((value, helper) => {
4670
if (value.match(/^(-?\d+) (minute|hour|day|week|month|quarter|year)s?$/)) {
4771
return value;
@@ -165,10 +189,11 @@ const BaseDimensionWithoutSubQuery = {
165189
})
166190
};
167191

168-
const BaseDimension = Object.assign({
192+
const BaseDimension = {
169193
subQuery: Joi.boolean().strict(),
170-
propagateFiltersToSubQuery: Joi.boolean().strict()
171-
}, BaseDimensionWithoutSubQuery);
194+
propagateFiltersToSubQuery: Joi.boolean().strict(),
195+
...BaseDimensionWithoutSubQuery
196+
};
172197

173198
const FixedRollingWindow = {
174199
type: Joi.string().valid('fixed'),
@@ -234,30 +259,6 @@ const BaseMeasure = {
234259
meta: Joi.any()
235260
};
236261

237-
function condition(fun, then, otherwise) {
238-
return Joi.alternatives().conditional(
239-
Joi.ref('.'), {
240-
is: Joi.custom((value, helper) => (fun(value) ? value : helper.message({}))),
241-
then,
242-
otherwise
243-
}
244-
);
245-
}
246-
247-
function defined(a) {
248-
return typeof a !== 'undefined';
249-
}
250-
251-
function inherit(a, b) {
252-
return Joi.object().keys(Object.assign({}, a, b));
253-
}
254-
255-
function requireOneOf(...keys) {
256-
return Joi.alternatives().try(
257-
...(keys.map((k) => Joi.object().keys({ [k]: Joi.exist().required() })))
258-
);
259-
}
260-
261262
const PreAggregationRefreshKeySchema = condition(
262263
(s) => defined(s.sql),
263264
Joi.object().keys({
@@ -605,6 +606,47 @@ const MeasuresSchema = Joi.object().pattern(identifierRegex, Joi.alternatives().
605606
]
606607
));
607608

609+
const DimensionsSchema = Joi.object().pattern(identifierRegex, Joi.alternatives().try(
610+
inherit(BaseDimensionWithoutSubQuery, {
611+
case: Joi.object().keys({
612+
when: Joi.array().items(Joi.object().keys({
613+
sql: Joi.func().required(),
614+
label: Joi.alternatives([
615+
Joi.string(),
616+
Joi.object().keys({
617+
sql: Joi.func().required()
618+
})
619+
])
620+
})),
621+
else: Joi.object().keys({
622+
label: Joi.alternatives([
623+
Joi.string(),
624+
Joi.object().keys({
625+
sql: Joi.func().required()
626+
})
627+
])
628+
})
629+
}).required()
630+
}),
631+
inherit(BaseDimensionWithoutSubQuery, {
632+
latitude: Joi.object().keys({
633+
sql: Joi.func().required()
634+
}).required(),
635+
longitude: Joi.object().keys({
636+
sql: Joi.func().required()
637+
}).required()
638+
}),
639+
inherit(BaseDimension, {
640+
sql: Joi.func().required(),
641+
}),
642+
inherit(BaseDimension, {
643+
multiStage: Joi.boolean().valid(true),
644+
type: Joi.any().valid('number').required(),
645+
sql: Joi.func().required(),
646+
addGroupBy: Joi.func(),
647+
})
648+
));
649+
608650
const SegmentsSchema = Joi.object().pattern(identifierRegex, Joi.object().keys({
609651
aliases: Joi.array().items(Joi.string()),
610652
sql: Joi.func().required(),
@@ -713,46 +755,7 @@ const baseSchema = {
713755
).required()
714756
})),
715757
measures: MeasuresSchema,
716-
dimensions: Joi.object().pattern(identifierRegex, Joi.alternatives().try(
717-
inherit(BaseDimensionWithoutSubQuery, {
718-
case: Joi.object().keys({
719-
when: Joi.array().items(Joi.object().keys({
720-
sql: Joi.func().required(),
721-
label: Joi.alternatives([
722-
Joi.string(),
723-
Joi.object().keys({
724-
sql: Joi.func().required()
725-
})
726-
])
727-
})),
728-
else: Joi.object().keys({
729-
label: Joi.alternatives([
730-
Joi.string(),
731-
Joi.object().keys({
732-
sql: Joi.func().required()
733-
})
734-
])
735-
})
736-
}).required()
737-
}),
738-
inherit(BaseDimensionWithoutSubQuery, {
739-
latitude: Joi.object().keys({
740-
sql: Joi.func().required()
741-
}).required(),
742-
longitude: Joi.object().keys({
743-
sql: Joi.func().required()
744-
}).required()
745-
}),
746-
inherit(BaseDimension, {
747-
sql: Joi.func().required()
748-
}),
749-
inherit(BaseDimension, {
750-
multiStage: Joi.boolean().valid(true),
751-
type: Joi.any().valid('number').required(),
752-
sql: Joi.func().required(),
753-
addGroupBy: Joi.func(),
754-
})
755-
)),
758+
dimensions: DimensionsSchema,
756759
segments: SegmentsSchema,
757760
preAggregations: PreAggregationsAlternatives,
758761
folders: Joi.array().items(Joi.object().keys({
@@ -765,14 +768,16 @@ const baseSchema = {
765768
accessPolicy: Joi.array().items(RolePolicySchema.required()),
766769
};
767770

771+
const hierarchySchema = Joi.object().pattern(identifierRegex, Joi.object().keys({
772+
title: Joi.string(),
773+
public: Joi.boolean().strict(),
774+
levels: Joi.func()
775+
}));
776+
768777
const cubeSchema = inherit(baseSchema, {
769778
sql: Joi.func(),
770779
sqlTable: Joi.func(),
771-
hierarchies: Joi.object().pattern(identifierRegex, Joi.object().keys({
772-
title: Joi.string(),
773-
public: Joi.boolean().strict(),
774-
levels: Joi.func()
775-
}))
780+
hierarchies: hierarchySchema,
776781
}).xor('sql', 'sqlTable').messages({
777782
'object.xor': 'You must use either sql or sqlTable within a model, but not both'
778783
});
@@ -803,7 +808,7 @@ const viewSchema = inherit(baseSchema, {
803808
})
804809
),
805810
accessPolicy: Joi.array().items(RolePolicySchema.required()),
806-
hierarchies: Joi.any()
811+
hierarchies: hierarchySchema,
807812
});
808813

809814
function formatErrorMessageFromDetails(explain, d) {

0 commit comments

Comments
 (0)