Validate negative offsets
This commit is contained in:
parent
90e1664ed6
commit
798b05c014
|
|
@ -653,4 +653,15 @@ Interval: 2:00 90%
|
|||
`),
|
||||
).toThrowErrorMatchingInlineSnapshot(`"Comment offset is larger than interval length at line 5 char 5"`);
|
||||
});
|
||||
|
||||
it("throws error when negative comment offset is outside of interval", () => {
|
||||
expect(() =>
|
||||
parse(`
|
||||
Name: My Workout
|
||||
Interval: 2:00 90%
|
||||
@ 0:00 Find your rythm.
|
||||
@ -3:10 Try to settle in for the effort
|
||||
`),
|
||||
).toThrowErrorMatchingInlineSnapshot(`"Negative comment offset is larger than interval length at line 5 char 5"`);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,15 +1,14 @@
|
|||
import { Workout, Interval, Comment } from "../ast";
|
||||
import { Workout, Interval } from "../ast";
|
||||
import { ValidationError } from "./ValidationError";
|
||||
|
||||
const isCommentWithinInterval = (comment: Comment, interval: Interval): boolean => {
|
||||
return comment.offset.seconds < interval.duration.seconds;
|
||||
};
|
||||
|
||||
const validateCommentOffsets = (interval: Interval) => {
|
||||
for (const comment of interval.comments) {
|
||||
if (!isCommentWithinInterval(comment, interval)) {
|
||||
if (comment.offset.seconds >= interval.duration.seconds) {
|
||||
throw new ValidationError(`Comment offset is larger than interval length`, comment.loc);
|
||||
}
|
||||
if (comment.offset.seconds < 0) {
|
||||
throw new ValidationError(`Negative comment offset is larger than interval length`, comment.loc);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue