// import * as rules from "./rules"; // export const validate = (validation, value) => { // let valid = true; // let rule; // let errors = []; // validation.forEach(({ action, message }) => { // // For file type. // /* NOTE: File validation needs alot of work */ // if (action === "file") { // if (value) { // value.map((file, i) => { // Object.keys(file).map(r => { // valid = rules[r].call(null, value[i], file[r]); // if (!valid) { // errors = [...errors, r]; // } // }); // }); // } // } else { // // For custom rule. // if (typeof action === "function") { // valid = action.call(); // rule = action.name; // } else { // const args = action.split(/:/g); // rule = args.shift(); // valid = rules[rule].call(null, value, args); // } // if (!valid) { // errors = [...errors, message]; // } // } // }); // return new Promise((resolve, reject) => { // return valid ? resolve( valid ) : reject( errors ); // // setTimeout(() => { // // return valid ? resolve( valid ) : reject( errors ); // // }, 1000); // }); // } import type { StreamQuery } from "../Models/stream.types"; import * as rules from "./rules"; export const runQuery = ( operator: string, value: any, anchorValues: any[] ): Promise => rules[ operator ].call(null, value, anchorValues); // TODO: Validation needs a ton of work. The auto-assignment here is hopefully temporary. export const validate = (validation: StreamQuery[] = [], value) => { // let valid = true; let errors = []; const promises = validation.map(({ operator, anchorValues, message }) => { // For file type. /* NOTE: File validation needs alot of work */ if (operator === "file") { // if (value) { // value.map((file, i) => { // Object.keys(file).map(r => { // valid = rules[r].call(null, value[i], file[r]); // if (!valid) { // errors = [...errors, r]; // } // }); // }); // } } else { // For custom rule. if (typeof operator === "function") { // valid = operator.call(); // rule = operator.name; } else { return runQuery( operator, value, anchorValues ); // rules[ operator ].call(null, value, anchorValues); } // if (!valid) { // errors = [...errors, message]; // } } }); return Promise.all(promises) .then((results: boolean[]) => { // TODO: As stated, this needs much thought. But for now: return !validation.length || ( !!results.length && results.every(val => !!val) ); // return !!results.length && results.every(val => !!val); }); // return new Promise((resolve, reject) => { // return valid ? resolve( valid ) : reject( errors ); // // setTimeout(() => { // // return valid ? resolve( valid ) : reject( errors ); // // }, 1000); // }); }