Skip to content

Commit 84e9304

Browse files
committed
additional timestamp content validation rules
1 parent 94e0c03 commit 84e9304

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

content-testing/schemas.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const baseVideosSchema = strictObject({
3838
title: string().required(),
3939
videoId: youtubeIdValidator.required(),
4040
nebulaSlug: nonUrlStringValidator,
41-
timestamps: timestampsArrayValidator.min(1).required()
41+
timestamps: timestampsArrayValidator.required()
4242
}).required()
4343
).min(2), // if we need parts, should have at least 2
4444

content-testing/validators.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,29 +208,56 @@ const timestampsArrayValidator = array(
208208
).test((values, context) => {
209209
if (!values) return true;
210210

211-
// first timestamp validation
211+
// Per https://support.google.com/youtube/answer/9884579?hl=en
212+
213+
// At least 3 timestamps are required
214+
if (values.length > 0 && values.length < 3) {
215+
return context.createError({
216+
message: 'At least 3 timestamps are required'
217+
});
218+
}
219+
220+
// first timestamp should be `0:00`
212221
if (values.length > 0 && timestampToSeconds(values[0].time) !== 0) {
213222
return context.createError({
214223
message: 'The first timestamp should be `0:00`'
215224
});
216225
}
217226

218-
// sequential timestamps
219-
let errors = [];
227+
// Timestamps should be sequential and at least 10 seconds apart from each other
228+
const sequenceErrors = [];
229+
const lengthErrors = [];
220230
let previousTime = -1;
221231

222232
for (const value of values) {
223233
const currentTime = timestampToSeconds(value.time);
224-
if (currentTime <= previousTime) errors.push(value);
234+
235+
if (currentTime <= previousTime) sequenceErrors.push(value);
236+
if (currentTime > 0 && currentTime - previousTime < 10)
237+
lengthErrors.push(value);
238+
225239
previousTime = currentTime;
226240
}
227241

228-
if (errors.length > 0) {
229-
const details = errors.map((v) => `${v.time} - ${v.title}`).join(' | ');
230-
const plural = errors.length > 1 ? 's are' : ' is';
242+
if (sequenceErrors.length > 0) {
243+
const details = sequenceErrors
244+
.map((v) => `${v.time} - ${v.title}`)
245+
.join(' | ');
246+
const plural = sequenceErrors.length > 1 ? 's are' : ' is';
247+
248+
return context.createError({
249+
message: `${sequenceErrors.length} timestamp${plural} not in sequential order | ${details}`
250+
});
251+
}
252+
253+
if (lengthErrors.length > 0) {
254+
const details = lengthErrors
255+
.map((v) => `${v.time} - ${v.title}`)
256+
.join(' | ');
257+
const plural = lengthErrors.length > 1 ? 's are' : ' is';
231258

232259
return context.createError({
233-
message: `${errors.length} timestamp${plural} not in sequential order | ${details}`
260+
message: `${lengthErrors.length} timestamp${plural} are less than 10 seconds after the previous one | ${details}`
234261
});
235262
}
236263

0 commit comments

Comments
 (0)