-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
116 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
app/adomin/routes/models/write/handle_special_fields_validation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { ColumnConfig, ModelConfig } from '#adomin/create_model_view_config' | ||
|
||
export type SpecialValidationResult = { error: string } | null | ||
|
||
export const handleSpecialFieldsValidation = async ( | ||
modelConfig: ModelConfig, | ||
parsedData: { | ||
[x: string]: any | ||
} | ||
): Promise<SpecialValidationResult> => { | ||
for (const field of modelConfig.fields) { | ||
if (field.adomin.type === 'json') { | ||
const res = await validateJsonField(field, parsedData) | ||
if (res) return res | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
const validateJsonField = async ( | ||
field: ColumnConfig, | ||
parsedData: any | ||
): Promise<SpecialValidationResult> => { | ||
if (field.adomin.type !== 'json') return null | ||
|
||
const parsedDataValue = parsedData[field.name] | ||
|
||
if (!parsedDataValue) return null | ||
|
||
try { | ||
const jsonParsedValue = JSON.parse(parsedDataValue) | ||
const res = await field.adomin.validation?.validate(jsonParsedValue) | ||
|
||
parsedData[field.name] = res | ||
} catch (error) { | ||
return { | ||
error: `Le champ ${field.name} (json) n'est pas valide`, | ||
} | ||
} | ||
|
||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters