2019-08-03 15:03:33 +02:00
const validate = { }
2018-12-18 15:25:19 +01:00
2021-01-31 09:22:15 +01:00
validate . point = function ( point , partId , pointId , debug ) {
2019-08-03 15:03:33 +02:00
if ( typeof point !== 'object' ) {
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'error' ,
label : 'Problem with point' ,
2021-04-24 10:16:31 +02:00
msg : points ,
2019-08-03 15:03:33 +02:00
} )
throw new Error ( ` Point pattern.parts. ${ partId } .points. ${ pointId } is not an object ` )
} else if ( typeof point . x !== 'number' || isNaN ( point . x ) ) {
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'error' ,
label : 'Problem with point X-value' ,
2021-04-24 10:16:31 +02:00
msg : points ,
2019-08-03 15:03:33 +02:00
} )
throw new Error ( ` X-value of point pattern.parts. ${ partId } .points. ${ pointId } is not a number ` )
} else if ( typeof point . y !== 'number' || isNaN ( point . y ) ) {
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'error' ,
label : 'Problem with point Y-value' ,
2021-04-24 10:16:31 +02:00
msg : points ,
2019-08-03 15:03:33 +02:00
} )
debug ( dbg , 'Problem with point Y-value:' , point )
throw new Error ( ` Y-value of point pattern.parts. ${ partId } .points. ${ pointId } is not a number ` )
2019-05-10 13:14:31 +02:00
} else if (
2019-08-03 15:03:33 +02:00
typeof point . attributes !== 'object' ||
2019-05-10 13:14:31 +02:00
! ( point . attributes . clone instanceof Function )
) {
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'error' ,
label : 'Problem with point attributes' ,
2021-04-24 10:16:31 +02:00
msg : points ,
2019-08-03 15:03:33 +02:00
} )
2019-05-10 13:14:31 +02:00
throw new Error (
` attributes property of point pattern.parts. ${ partId } .points. ${ pointId } is not an object `
2019-08-03 15:03:33 +02:00
)
2019-05-10 13:14:31 +02:00
} else if ( ! ( point . clone instanceof Function ) ) {
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'error' ,
label : 'Problem with point' ,
2021-04-24 10:16:31 +02:00
msg : points ,
2019-08-03 15:03:33 +02:00
} )
throw new Error ( ` Point pattern.parts. ${ partId } .points. ${ pointId } is not a valid Point object ` )
2018-12-18 15:25:19 +01:00
}
2019-08-03 15:03:33 +02:00
return true
}
2018-12-18 15:25:19 +01:00
2021-01-31 09:22:15 +01:00
validate . text = function ( type , item , partId , itemId , debug ) {
2019-08-03 15:03:33 +02:00
let text = item . attributes . getAsArray ( 'data-text' )
if ( text === false ) return true
2018-12-18 15:25:19 +01:00
else {
2019-08-03 15:03:33 +02:00
if ( item . attributes . get ( 'data-validate-skip-text' ) !== false ) {
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'warning' ,
label : '🌐 Possible translation issue' ,
2021-04-24 10:16:31 +02:00
msg : ` This text might be a translation problem:, ${ item } However, the error was suppresed, so moving on. ` ,
2019-08-03 15:03:33 +02:00
} )
return true
2018-12-18 15:25:19 +01:00
}
for ( let t of text ) {
2019-08-03 15:03:33 +02:00
if ( typeof t !== 'string' && typeof t !== 'number' ) {
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'error' ,
label : 'This text is not a string or number' ,
2021-04-24 10:16:31 +02:00
msg : t ,
2019-08-03 15:03:33 +02:00
} )
2019-05-10 13:14:31 +02:00
throw new Error (
` ${ type } pattern.parts. ${ partId } . ${ type } s. ${ itemId } has text that is not a string nor a number. Set the 'data-validate-skip-text' attribute to true to suppress this error. `
2019-08-03 15:03:33 +02:00
)
} else if ( typeof t === 'string' && t . indexOf ( ' ' ) !== - 1 ) {
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'warning' ,
label : '🌐 Possible translation issue' ,
2021-04-24 10:16:31 +02:00
msg : t ,
2019-08-03 15:03:33 +02:00
} )
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'info' ,
label : '💡 Tip' ,
2021-04-24 10:16:31 +02:00
msg : ` ${ type } pattern.parts. ${ partId } . ${ type } s. ${ itemId } has text containing spaces. Please insert translation identifiers, and not actual text. Set the 'data-validate-skip-text' attribute to true to suppress this warning. ` ,
2019-08-03 15:03:33 +02:00
} )
2018-12-18 15:25:19 +01:00
}
}
}
2019-08-03 15:03:33 +02:00
return true
}
2018-12-18 15:25:19 +01:00
2021-01-31 09:22:15 +01:00
validate . path = function ( path , partId , pathId , debug ) {
2019-08-03 15:03:33 +02:00
if ( typeof path !== 'object' ) {
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'error' ,
label : 'Problem with path' ,
2021-04-24 10:16:31 +02:00
msg : path ,
2019-08-03 15:03:33 +02:00
} )
throw new Error ( ` Path pattern.parts. ${ partId } .paths. ${ pathId } is not an object ` )
} else if ( typeof path . ops !== 'object' ) {
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'error' ,
label : 'Problem with path ops' ,
2021-04-24 10:16:31 +02:00
msg : path ,
2019-08-03 15:03:33 +02:00
} )
throw new Error ( ` ops property of path pattern.parts. ${ partId } .paths. ${ pathId } is not an object ` )
2019-05-10 13:14:31 +02:00
} else if ( path . ops . length < 2 ) {
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'error' ,
label : 'Problem with path ops' ,
2021-04-24 10:16:31 +02:00
msg : path ,
2019-08-03 15:03:33 +02:00
} )
throw new Error ( ` Path pattern.parts. ${ partId } .paths. ${ pathId } does not do anything ` )
} else if ( typeof path . attributes !== 'object' ) {
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'error' ,
label : 'Problem with path attributes' ,
2021-04-24 10:16:31 +02:00
msg : path ,
2019-08-03 15:03:33 +02:00
} )
2019-05-10 13:14:31 +02:00
throw new Error (
` attributes property of path pattern.parts. ${ partId } .paths. ${ pathId } is not an object `
2019-08-03 15:03:33 +02:00
)
2019-05-10 13:14:31 +02:00
} else if ( ! ( path . clone instanceof Function ) ) {
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'error' ,
label : 'Problem with path' ,
2021-04-24 10:16:31 +02:00
msg : path ,
2019-08-03 15:03:33 +02:00
} )
throw new Error ( ` Path pattern.parts. ${ partId } .paths. ${ pathId } is not a valid Path object ` )
2019-05-10 13:14:31 +02:00
} else if ( ! ( path . attributes . clone instanceof Function ) ) {
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'error' ,
label : 'Problem with path attributes' ,
2021-04-24 10:16:31 +02:00
msg : path ,
2019-08-03 15:03:33 +02:00
} )
2019-05-10 13:14:31 +02:00
throw new Error (
` attributes property of path pattern.parts. ${ partId } .paths. ${ pathId } is not a valid Attributes object `
2019-08-03 15:03:33 +02:00
)
2018-12-18 15:25:19 +01:00
}
for ( let o in path . ops ) {
2019-08-03 15:03:33 +02:00
let op = path . ops [ o ]
if ( op . type !== 'close' ) {
if ( ! validate . point ( op . to , partId , '_unknown_' , debug ) ) {
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'error' ,
label : 'Problem with path TO point' ,
2021-04-24 10:16:31 +02:00
msg : op . to ,
2019-08-03 15:03:33 +02:00
} )
2019-05-10 13:14:31 +02:00
throw new Error (
` Point in pattern.parts. ${ partId } .paths. ${ pathId } .ops[o].to is not a valid Point object `
2019-08-03 15:03:33 +02:00
)
2018-12-18 15:25:19 +01:00
}
2019-08-03 15:03:33 +02:00
} else if ( op . type === 'curve' ) {
if ( ! validate . point ( op . cp1 , partId , '_unknown_' , debug ) ) {
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'error' ,
label : 'Problem with path CP1 point' ,
2021-04-24 10:16:31 +02:00
msg : op . cp1 ,
2019-08-03 15:03:33 +02:00
} )
2019-05-10 13:14:31 +02:00
throw new Error (
` Point in pattern.parts. ${ partId } .paths. ${ pathId } .ops[o].cp1 is not a valid Point object `
2019-08-03 15:03:33 +02:00
)
} else if ( ! validate . point ( op . cp2 , partId , '_unknown_' , debug ) ) {
2018-12-18 15:25:19 +01:00
debug ( {
2019-08-03 15:03:33 +02:00
type : 'error' ,
label : 'Problem with path CP2 point' ,
2021-04-24 10:16:31 +02:00
msg : op . cp2 ,
2019-08-03 15:03:33 +02:00
} )
2019-05-10 13:14:31 +02:00
throw new Error (
` Point in pattern.parts. ${ partId } .paths. ${ pathId } .ops[o].cp2 is not a valid Point object `
2019-08-03 15:03:33 +02:00
)
2018-12-18 15:25:19 +01:00
}
}
}
2019-08-03 15:03:33 +02:00
return true
}
2018-12-18 15:25:19 +01:00
2021-01-31 09:22:15 +01:00
validate . snippet = function ( snippet , partId , snippetId , debug ) {
2019-08-03 15:03:33 +02:00
if ( typeof snippet !== 'object' ) return false
if ( ! validate . point ( snippet . anchor , partId , '_unknown_' , debug ) ) return false
if ( typeof snippet . attributes !== 'object' ) return false
if ( ! ( snippet . clone instanceof Function ) ) return false
if ( ! ( snippet . attributes . clone instanceof Function ) ) return false
2018-12-18 15:25:19 +01:00
2019-08-03 15:03:33 +02:00
return true
}
2018-12-18 15:25:19 +01:00
2019-08-03 15:03:33 +02:00
export default validate