form
All checks were successful
deploy to production / deploy (push) Successful in 49s

This commit is contained in:
2023-09-24 09:38:03 +00:00
parent 7813f0b486
commit eee191955e
32 changed files with 1091 additions and 518 deletions

View File

@@ -1,13 +1,14 @@
const { validateFields } = require("./validateFields")
;(function () {
if (context.data.formular.honey) {
if (context?.data?.formular?.honey) {
throw { status: 400, error: "Bot detection" }
}
delete context.data.formular.honey
let values = Object.entries(context.data.formular)
/**
* @type {Array<[string, FormObj]>}
*/
let values = Object.entries(context?.data?.formular)
let validation = validateFields(values)
console.log(validation)
if (validation.length) {
throw { status: 400, error: validation }
}

View File

@@ -1,35 +1,54 @@
var utils = require("../lib/utils")
var config = require("../config")
;(function () {
let formular = context.data.formular
/** @type {FormObj} */
let formular = context?.data?.formular
/** @type {TempFormBefore | TempFormAfter} */
let tempForm = {}
let formTitle = `${formular.formTitle}`
delete formular.formTitle
delete formular["agreement"]
delete formular["honey"]
formular.formRows.forEach((rowName, i) => {
tempForm[rowName] = {}
})
formular?.formRows?.forEach(
/** @param {string} rowName */
(rowName) => {
tempForm[rowName] = {}
}
)
/**
* @param {[string, any]} e
* @returns {any}
*/
let getValue = (e) => {
if (e[0].includes("numberLabel")) return e[1][0]
return e[1]
}
/** @type {TempFormIndices} */
let indices = {}
Object.entries(formular).forEach((e) => {
if (e[0] == "formRows") return
if (e[0] === "formRows") return
let key = e[0].split("_")
let newKey = key[key.length - 1]
let rowName = formular.formRows[Number(key[key.length - 2])]
// @ts-ignore
let rowName = formular?.formRows[Number(key[key.length - 2])]
let index = isNaN(Number(key[key.length - 3])) ? 100 : Number(key[key.length - 3])
if (!rowName) return
if (!tempForm[rowName]) tempForm[rowName] = {}
// @ts-ignore
if (tempForm[rowName][newKey]) {
// @ts-ignore
tempForm[rowName][newKey][0] += "\\" + getValue(e)
} else {
tempForm[rowName][newKey] = [getValue(e), newKey[0] == "n", newKey.includes("invalid")]
// @ts-ignore
tempForm[rowName][newKey] = [getValue(e), newKey[0] === "n", newKey.includes("invalid")]
indices[rowName] = { ...indices[rowName], [newKey]: index }
}
})
@@ -40,12 +59,12 @@ var config = require("../config")
.sort((a, b) => indices[rowName][a[0]] - indices[rowName][b[0]])
.reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {})
})
console.log(JSON.stringify(tempForm), JSON.stringify(indices))
Object.keys(tempForm).forEach((row) => {
tempForm[row] = Object.entries(tempForm[row])
})
// @ts-ignore
delete tempForm[undefined]
context.smtp.sendMail({
to: "binkrassdufass@gmail.com",

View File

@@ -1,5 +1,11 @@
/**
* @param {Array<[string, FormObj]>} fieldsArray
* @returns {(string | (() => void))[][]}
*/
function validateFields(fieldsArray) {
/**@type {(string | (() => void))[][]} */
const errors = []
/**@type {number} */
let selectedGroup
const numberRegex = /^[+]?([.]\d+|\d+([.]\d+)?)$/
@@ -14,6 +20,12 @@ function validateFields(fieldsArray) {
blockContainer.classList.add("invalidBlocks")
}
/**
*
* @param {string} value
* @param {any} field
* @param {HTMLElement} element
*/
const validateNumber = (value, field, element) => {
if (!numberRegex.test(`${value}`)) {
errors.push(["block", () => element.classList.add("border-red")])
@@ -23,6 +35,7 @@ function validateFields(fieldsArray) {
fieldsArray.forEach(([field, value]) => {
if (field === "blockGroups" || field.includes("numberLabel")) {
if (!field.includes("numberLabel")) return
// @ts-ignore
const [elementValue, element, group] = value
if (!elementValue) return