apxtrib/asupsetup/data/domain/apixpress/www/cdn/public/js/checkdata.js

186 lines
5.8 KiB
JavaScript
Raw Normal View History

2023-01-22 09:53:09 +00:00
/*
This module have to be independant of any external package
it is shared between back and front and is usefull
to apply common check in front before sending it in back
can be include in project with
2023-04-13 05:46:35 +00:00
<script src="https://apiback.maildigit.fr/js/Checkjson.js"></script>
or with const Checkjson = require('../public/js/Checkjson.js')
2023-01-22 09:53:09 +00:00
*/
// --##
2023-04-13 05:46:35 +00:00
const Checkjson = {};
// each Checkjson.test. return true or false
Checkjson.test = {};
2023-01-22 09:53:09 +00:00
2023-04-13 05:46:35 +00:00
Checkjson.test.emailadress = ( ctx, email ) => {
2023-01-22 09:53:09 +00:00
const regExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return regExp.test( email );
};
/*
* @emaillist = "email1,email2, email3"
* it check if each eamil separate by , are correct
*/
2023-04-13 05:46:35 +00:00
Checkjson.test.emailadresslist = ( ctx, emaillist ) => {
2023-01-22 09:53:09 +00:00
//console.log(emaillist.split(','))
if( emaillist.length > 0 ) {
const emails = emaillist.split( ',' );
for( var i in emails ) {
//console.log(emails[i])
2023-04-13 05:46:35 +00:00
if( !Checkjson.test.emailadress( "", emails[ i ].trim() ) ) {
2023-01-22 09:53:09 +00:00
return false
}
}
};
return true;
};
2023-04-13 05:46:35 +00:00
Checkjson.test.password = ( ctx, pwd ) => {
2023-01-22 09:53:09 +00:00
const regExp = new RegExp(
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&.])[A-Za-z\d$@$!%*?&.{}:|\s]{8,}/
);
return regExp.test( pwd );
};
2023-04-13 05:46:35 +00:00
Checkjson.test.required = ( ctx, val ) =>
2023-01-22 09:53:09 +00:00
( val != null && val != 'undefined' && val.length > 0 ) || ( !!val && val.constructor === Array && val.length > 0 ) || ( !!val && val.constructor === Object && Object.keys( val )
.length > 0 );
2023-04-13 05:46:35 +00:00
Checkjson.test.isNumber = ( ctx, n ) => typeof n === 'number';
Checkjson.test.isInt = ( ctx, n ) => n != '' && !isNaN( n ) && Math.round( n ) == n;
Checkjson.test.isFloat = ( ctx, n ) => n != '' && !isNaN( n ) && Math.round( n ) != n;
Checkjson.test.unique = ( ctx, val ) => {
2023-01-22 09:53:09 +00:00
if( ctx.list[ ctx.currentfield ] ) {
return !ctx.list[ ctx.currentfield ].includes( val );
} else {
console.log( 'ERR no list for field:' + ctx.currentfield );
return false;
}
};
2023-04-13 05:46:35 +00:00
Checkjson.test.isDateDay = ( ctx, dateDay ) => true;
/* Checkjson.test.filterInvalidInArray = (array, validate) =>
2023-01-22 09:53:09 +00:00
array ? array.filter(el => !validate(el)) : true;
// return true when every elements is valid
*/
2023-04-13 05:46:35 +00:00
Checkjson.test.postalCode = ( ctx, postalCode ) => {
2023-01-22 09:53:09 +00:00
if( postalCode.length == 0 ) return true;
const regExp = new RegExp( /(^\d{5}$)|(^\d{5}-\d{4}$)/ );
return regExp.test( postalCode );
};
/**
* PHONE
*/
2023-04-13 05:46:35 +00:00
Checkjson.test.phoneNumber = ( ctx, phoneNumber ) => {
2023-01-22 09:53:09 +00:00
if( phoneNumber.length == 0 ) return true;
phoneNumber = phoneNumber.trim()
.replace( /[- .]/g, '' )
//french number
const regExpfr = new RegExp( /^0[1-9][0-9]{9}$/ );
const regExpInternational = new RegExp( /^\+*(\d{3})*[0-9,\-]{8,}/ );
return regExpfr.test( phoneNumber ) || regExpInternational.test( phoneNumber );
};
/*
* @phonelist = "phone1,phone2,phone3"
* it check if each phone separate by , are correct
*/
2023-04-13 05:46:35 +00:00
Checkjson.test.phoneNumberlist = ( ctx, phonelist ) => {
2023-01-22 09:53:09 +00:00
//console.log(emaillist.split(','))
if( phonelist.length > 0 ) {
const phones = phonelist.split( ',' );
for( var i in phones ) {
//console.log(emails[i])
2023-04-13 05:46:35 +00:00
if( !Checkjson.test.phoneNumber( "", phones[ i ].trim() ) ) {
2023-01-22 09:53:09 +00:00
return false
}
}
};
return true;
};
2023-04-13 05:46:35 +00:00
// Checkjson.normalize take a correct data then reformat it to harmonise it
Checkjson.normalize = {};
Checkjson.normalize.phoneNumber = ( ctx, phone ) => {
2023-01-22 09:53:09 +00:00
phone = phone.trim()
.replace( /[- .]/g, '' );
2023-04-13 05:46:35 +00:00
if( Checkjson.test.phoneNumber( '', phone ) && phone.length == 10 && phone[ 0 ] == "0" ) {
2023-01-22 09:53:09 +00:00
phone = '+33 ' + phone.substring( 1 );
}
return phone;
}
2023-04-13 05:46:35 +00:00
Checkjson.normalize.upperCase = ( ctx, txt ) => txt.toUpperCase();
Checkjson.normalize.lowerCase = ( ctx, txt ) => txt.toLowerCase();
2023-01-22 09:53:09 +00:00
// fixe 10 position et complete par des 0 devant
2023-04-13 05:46:35 +00:00
Checkjson.normalize.zfill10 = ( ctx, num ) => {
2023-01-22 09:53:09 +00:00
let s = num + '';
while( s.length < 10 ) s = '0' + s;
return s;
};
/*let tt = "+33 1 02.03 04 05";
2023-04-13 05:46:35 +00:00
console.log(Checkjson.test.phoneNumber('', tt))
console.log(Checkjson.normalize.phoneNumber('', tt))
2023-01-22 09:53:09 +00:00
*/
2023-04-13 05:46:35 +00:00
Checkjson.evaluate = ( contexte, referential, data ) => {
2023-01-22 09:53:09 +00:00
/*
* contexte object {} with full info for evaluation
* file referential path to get object to apply
* data related to object
- return {validefor =[keyword of error] if empty no error,
clean data eventually reformated
updateDatabase}
*/
console.log( 'contexte', contexte );
console.log( 'referentiel', referential );
console.log( 'data', data );
const invalidefor = [];
const objectdef = {};
const listfield = referential.map( ch => {
objectdef[ ch.idfield ] = ch;
return ch.idfield;
} );
Object.keys( data )
.forEach( field => {
if( !listfield.includes( field ) ) {
// some data can be inside an object with no control at all
// they are used for process only
// i leave it in case it will become a non sens
// invalidefor.push('ERRFIELD unknown of referentials ' + field);
} else {
if( objectdef[ field ].check ) {
// check data with rule list in check
2023-04-13 05:46:35 +00:00
objectdef[ field ].Checkjson.forEach( ctrl => {
2023-01-22 09:53:09 +00:00
console.log( 'ctrl', ctrl );
contexte.currentfield = field;
2023-04-13 05:46:35 +00:00
if( !Checkjson.test[ ctrl ] ) {
2023-01-22 09:53:09 +00:00
invalidefor.push( 'ERR check function does not exist :' + ctrl + '___' + field )
} else {
2023-04-13 05:46:35 +00:00
if( !Checkjson.test[ ctrl ]( contexte, data[ field ] ) )
2023-01-22 09:53:09 +00:00
invalidefor.push( 'ERR' + ctrl + '___' + field );
}
} );
}
if( objectdef[ field ].nouserupdate ) {
// check if user can modify this information
console.log(
'evaluation :' + field + ' -- ' + objectdef[ field ].nouserupdate,
eval( objectdef[ field ].nouserupdate )
);
const evalright = eval( objectdef[ field ].nouserupdate );
objectdef[ field ].nouserupdate = evalright;
}
}
} );
console.log( {
invalidefor,
data
} );
return {
invalidefor,
data
};
};
2023-04-13 05:46:35 +00:00
if( typeof module !== 'undefined' ) module.exports = Checkjson;