2018-08-10 16:03:39 +02:00
import { version , name } from "../package.json" ;
2018-09-05 16:38:51 +02:00
const dbg = {
debug : 'custom' ,
text : 'Validation error' ,
style : 'color: red; font-weight: bold;'
}
function validatePoint ( point , partId , pointId , debug ) {
if ( typeof point !== 'object' ) {
debug ( dbg , 'Problem with point:' , point ) ;
throw ( ` Point pattern.parts. ${ partId } .points. ${ pointId } is not an object ` ) ;
} else if ( typeof point . x !== 'number' || isNaN ( point . x ) ) {
debug ( dbg , 'Problem with point X-value:' , point ) ;
throw ( ` X-value of point pattern.parts. ${ partId } .points. ${ pointId } is not a number ` ) ;
} else if ( typeof point . y !== 'number' || isNaN ( point . y ) ) {
debug ( dbg , 'Problem with point Y-value:' , point ) ;
throw ( ` Y-value of point pattern.parts. ${ partId } .points. ${ pointId } is not a number ` ) ;
} else if ( typeof point . attributes !== 'object' || ! point . attributes . clone instanceof Function ) {
debug ( dbg , 'Problem with point attributes:' , point ) ;
throw ( dbg , ` attributes property of point pattern.parts. ${ partId } .points. ${ pointId } is not an object ` ) ;
} else if ( ! point . clone instanceof Function ) {
debug ( dbg , 'Problem with point:' , point ) ;
throw ( ` Point pattern.parts. ${ partId } .points. ${ pointId } is not a valid Point object ` ) ;
}
return true ;
}
2018-09-05 16:47:02 +02:00
function validateText ( type , item , partId , itemId , debug ) {
let text = item . attributes . getAsArray ( 'data-text' ) ;
2018-09-05 16:38:51 +02:00
if ( text === false ) return true ;
else {
2018-09-05 16:47:02 +02:00
if ( item . attributes . get ( 'data-validate-skip-text' ) !== false ) {
2018-09-05 16:38:51 +02:00
debug (
{
debug : 'custom' ,
text : 'Validation warning' ,
style : 'color: #da0; font-weight: bold;'
} ,
'This text might be a translation problem:' ,
2018-09-05 16:47:02 +02:00
item ,
2018-09-05 16:38:51 +02:00
'However, the error was suppresed, so moving on.'
) ;
return true ;
}
for ( let t of text ) {
if ( typeof t !== 'string' && typeof t !== 'number' ) {
debug ( dbg , 'This text is not a string or number:' , t ) ;
2018-09-05 16:47:02 +02:00
throw ( ` ${ 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. ` ) ;
2018-09-05 16:38:51 +02:00
} else if ( typeof t === 'string' && t . indexOf ( ' ' ) !== - 1 ) {
2018-09-05 16:47:02 +02:00
debug ( dbg , 'This text might be a translation problem:' , item ) ;
throw ( ` ${ 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 error. ` ) ;
2018-09-05 16:38:51 +02:00
}
}
}
return true ;
}
function validatePath ( path , partId , pathId , debug ) {
if ( typeof path !== 'object' ) {
debug ( dbg , 'Problem with path:' , path ) ;
throw ( ` Path pattern.parts. ${ partId } .paths. ${ pathId } is not an object ` ) ;
} else if ( typeof path . ops !== 'object' ) {
debug ( dbg , 'Problem with path ops:' , path ) ;
throw ( ` ops property of path pattern.parts. ${ partId } .paths. ${ pathId } is not an object ` ) ;
} else if ( path . ops . length < 2 ) {
debug ( dbg , 'Problem with path ops:' , path ) ;
throw ( ` Path pattern.parts. ${ partId } .paths. ${ pathId } does not do anything ` ) ;
} else if ( typeof path . attributes !== 'object' ) {
debug ( dbg , 'Problem with path attributes:' , path ) ;
throw ( ` attributes property of path pattern.parts. ${ partId } .paths. ${ pathId } is not an object ` ) ;
} else if ( ! path . clone instanceof Function ) {
debug ( dbg , 'Problem with path:' , path ) ;
throw ( ` Path pattern.parts. ${ partId } .paths. ${ pathId } is not a valid Path object ` ) ;
} else if ( ! path . attributes . clone instanceof Function ) {
debug ( dbg , 'Problem with path attributes:' , path ) ;
throw ( ` attributes property of path pattern.parts. ${ partId } .paths. ${ pathId } is not a valid Attributes object ` ) ;
}
for ( let o in path . ops ) {
let op = path . ops [ o ] ;
if ( op . type !== 'close' ) {
if ( ! validatePoint ( op . to , partId , '_unknown_' , debug ) ) {
debug ( dbg , 'Problem with path TO point:' , op . to ) ;
throw ( ` Point in pattern.parts. ${ partId } .paths. ${ pathId } .ops[o].to is not a valid Point object ` ) ;
}
} else if ( op . type === 'curve' ) {
if ( ! validatePoint ( op . cp1 , partId , '_unknown_' , debug ) ) {
debug ( dbg , 'Problem with path CP1 point:' , op . cp1 ) ;
throw ( ` Point in pattern.parts. ${ partId } .paths. ${ pathId } .ops[o].cp1 is not a valid Point object ` ) ;
} else if ( ! validatePoint ( op . cp2 , partId , '_unknown_' , debug ) ) {
debug ( dbg , 'Problem with path CP2 point:' , op . cp1 ) ;
throw ( ` Point in pattern.parts. ${ partId } .paths. ${ pathId } .ops[o].cp2 is not a valid Point object ` ) ;
}
}
}
return true ;
}
function isSnippet ( snippet , partId , snippetId , debug ) {
if ( typeof snippet !== 'object' ) return false ;
if ( ! validatePoint ( 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 ;
return true ;
}
2018-08-10 16:03:39 +02:00
export default {
name : name ,
version : version ,
hooks : {
preDraft : function ( next ) {
for ( let m in this . config . measurements ) {
let measurement = this . config . measurements [ m ] ;
if ( ! this . context . settings . measurements [ measurement ] ) {
2018-09-05 16:38:51 +02:00
this . debug ( dbg , 'Missing measurement:' , measurement ) ;
2018-08-10 16:03:39 +02:00
this . debug ( 'All measurements:' , this . settings . measurements ) ;
throw ` Missing measurement: ${ measurement } ` ;
}
}
next ( ) ;
2018-09-05 16:38:51 +02:00
} ,
postDraft : function ( next ) {
for ( let partId in this . parts ) {
let part = this . parts [ partId ] ;
let { debug } = part . shorthand ( ) ;
for ( let pointId in part . points ) {
validatePoint ( part . points [ pointId ] , partId , pointId , debug ) ;
2018-09-05 16:47:02 +02:00
validateText ( 'point' , part . points [ pointId ] , partId , pointId , debug ) ;
2018-09-05 16:38:51 +02:00
}
for ( let pathId in part . paths ) {
validatePath ( part . paths [ pathId ] , partId , pathId , debug ) ;
2018-09-05 16:47:02 +02:00
validateText ( 'path' , part . paths [ pathId ] , partId , pathId , debug ) ;
2018-09-05 16:38:51 +02:00
}
for ( let snippetId in part . snippets ) {
if ( ! isSnippet ( part . snippets [ snippetId ] , partId , snippetId , debug ) ) {
throw ( ` pattern.parts. ${ partId } .snippets. ${ snippetId } is not a valid Snippet object ` ) ;
}
}
}
next ( ) ;
2018-08-10 16:03:39 +02:00
}
}
} ;