2023-10-03 00:31:27 -04:00
import * as yup from "yup" ;
2023-12-29 20:11:07 -05:00
import { FormChapter } from "./types/form/story" ;
2023-10-03 00:31:27 -04:00
const emptySummary = "Summary cannot be blank" ;
const emptyChapterTitle = "Chapter title cannot be blank." ;
const blankTitle = "Title cannot be blank." ;
export const cs = yup . object < FormChapter > ( ) . shape ( {
2023-12-29 20:11:07 -05:00
chapterTitle : yup.string ( ) . ensure ( ) . min ( 1 , emptyChapterTitle ) . trim ( emptyChapterTitle ) . required ( emptyChapterTitle ) ,
2023-10-03 00:31:27 -04:00
summary : yup.string ( ) . ensure ( ) . min ( 10 , emptySummary ) . required ( emptySummary ) ,
notes : yup.string ( ) . ensure ( ) ,
2023-12-29 20:11:07 -05:00
bands : yup.array ( ) . ensure ( ) . of ( yup . number ( ) ) . min ( 1 , "One or more bands must be selected." ) ,
characters : yup.array ( ) . ensure ( ) . min ( 1 , "One or more characters must be selected" ) ,
2023-10-03 00:31:27 -04:00
relationships : yup
. array ( )
. ensure ( )
2023-12-29 20:11:07 -05:00
. of ( yup . array ( ) . ensure ( ) . of ( yup . string ( ) ) . min ( 2 , "Pairings must have at least two characters!" ) . max ( 3 , "Pairings can have no more than three characters!" ) ) ,
2023-10-03 00:31:27 -04:00
nsfw : yup.boolean ( ) . oneOf ( [ true , false ] ) ,
loggedInOnly : yup.boolean ( ) . when ( "nsfw" , ( [ nsfw ] , schema ) = > {
return nsfw
? schema . oneOf (
[ true ] ,
"If your story contains adult content, you MUST restrict the ability to read it to logged-in users only. Failure to comply may result in a takedown." ,
2023-12-28 20:05:16 -05:00
)
2023-10-03 00:31:27 -04:00
: schema . oneOf ( [ true , false ] ) ;
} ) ,
hidden : yup.boolean ( ) . oneOf ( [ true , false ] ) ,
2023-12-29 20:11:07 -05:00
pot : yup.string ( ) . oneOf ( [ "pasteOrType" , "upload" ] ) . required ( "Story content is required!" ) ,
2023-10-03 00:31:27 -04:00
storytext : yup.string ( ) . when ( "pot" , ( [ pot ] , schema ) = > {
return pot === "pasteOrType"
? schema
2023-12-29 20:11:07 -05:00
. test ( "numWords" , "Story must be at least 50 words" , ( value : any , context ) = > {
return value ? . split ( /\W+/ ) . length > 50 || false ;
} )
2023-10-03 00:31:27 -04:00
. required ( "Story text can't be blank!" )
: schema . min ( 0 ) ;
} ) ,
// hello Celeste
file : yup.mixed ( ) . when (
"pot" ,
( [ pot ] , schema ) = > {
return pot === "upload"
? yup . string ( ) . ensure ( ) . trim ( ) . required ( "file name required" )
: / * . m i x e d ( )
. test ( "exists" , "You need to upload a file!" , ( value ) = > {
// console.debug("file: ", value)
return ! ! value ;
} )
. test (
"fileType" ,
"Supported files are *.docx and *.doc" ,
( value ) = > {
let ext ;
if ( typeof value == "string" ) {
ext = value ? . split ( "." ) . reverse ( ) [ 0 ] ;
} else {
ext = ( value as File ) ? . name ? . split ( "." ) . reverse ( ) [ 0 ] ;
}
2023-10-10 22:26:17 -04:00
//
2023-10-03 00:31:27 -04:00
let reg = /(docx|doc)$/i ;
return reg . test ( ext ) ;
} ,
) * /
2023-12-28 20:05:16 -05:00
yup . mixed ( ) ;
2023-10-03 00:31:27 -04:00
} / * {
is : "upload" ,
then : ,
otherwise : yup.mixed ( )
} * / ,
) ,
} ) ;
export const storySchema = yup . object ( ) . shape ( {
title : yup.string ( ) . ensure ( ) . min ( 5 , blankTitle ) . required ( blankTitle ) ,
2023-12-29 20:11:07 -05:00
chapters : yup.array ( ) . min ( 1 , "There must be at least one chapter." ) . of ( cs ) . ensure ( ) ,
2023-10-03 00:31:27 -04:00
completed : yup.boolean ( ) . oneOf ( [ true , false ] ) ,
} ) ;