Take in functions as defaults

This commit is contained in:
1ilit 2024-04-24 11:49:00 +03:00
parent ca8ee61ee9
commit 2751a6d6df
3 changed files with 9 additions and 3 deletions

View File

@ -1,4 +1,4 @@
import { strHasQuotes } from "./utils"; import { isFunction, strHasQuotes } from "./utils";
function validateDateStr(str) { function validateDateStr(str) {
return /^(?!0000)(?!00)(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[01])|(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31))$/.test( return /^(?!0000)(?!00)(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9]|3[01])|(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31))$/.test(
@ -9,6 +9,8 @@ function validateDateStr(str) {
function checkDefault(field) { function checkDefault(field) {
if (field.default === "") return true; if (field.default === "") return true;
if (isFunction(field.default)) return true;
switch (field.type) { switch (field.type) {
case "INT": case "INT":
case "BIGINT": case "BIGINT":

View File

@ -1,5 +1,5 @@
import { sqlDataTypes } from "../data/constants"; import { sqlDataTypes } from "../data/constants";
import { strHasQuotes } from "./utils"; import { isFunction, strHasQuotes } from "./utils";
export function getJsonType(f) { export function getJsonType(f) {
if (!sqlDataTypes.includes(f.type)) { if (!sqlDataTypes.includes(f.type)) {
@ -144,7 +144,7 @@ export function hasQuotes(type) {
} }
export function parseDefault(field) { export function parseDefault(field) {
if (strHasQuotes(field.default)) { if (strHasQuotes(field.default) || isFunction(field.default)) {
return field.default; return field.default;
} }

View File

@ -24,3 +24,7 @@ export function strHasQuotes(str) {
(str[0] === str[str.length - 1] && str[0] === "`") (str[0] === str[str.length - 1] && str[0] === "`")
); );
} }
export function isFunction(str) {
return /\w+\([^)]*\)$/.test(str);
}