json schema

This commit is contained in:
muzam1l
2022-05-05 20:00:10 +05:30
parent 386619619b
commit 866f6257f1
7 changed files with 232 additions and 51 deletions

View File

@@ -3,6 +3,7 @@ import { deepEqual } from '../utils/object';
import transactionsData from "../content/transactions.json";
import state from '.';
import { showAlert } from "../state/actions/showAlert";
import { parseJSON } from '../utils/json';
export type SelectOption = {
value: string;
@@ -106,25 +107,25 @@ export const prepareTransaction = (data: any) => {
(Object.keys(options)).forEach(field => {
let _value = options[field];
// convert xrp
if (_value && typeof _value === "object" && _value.type === "xrp") {
if (+_value.value) {
options[field] = (+_value.value * 1000000 + "") as any;
if (_value && typeof _value === "object" && _value.$type === "xrp") {
if (+_value.$value) {
options[field] = (+_value.$value * 1000000 + "") as any;
} else {
options[field] = undefined; // 👇 💀
}
}
// handle type: `json`
if (_value && typeof _value === "object" && _value.type === "json") {
if (typeof _value.value === "object") {
options[field] = _value.value as any;
if (_value && typeof _value === "object" && _value.$type === "json") {
if (typeof _value.$value === "object") {
options[field] = _value.$value as any;
} else {
try {
options[field] = JSON.parse(_value.value);
options[field] = JSON.parse(_value.$value);
} catch (error) {
const message = `Input error for json field '${field}': ${error instanceof Error ? error.message : ""
}`;
console.error(message)
options[field] = _value.value
options[field] = _value.$value
}
}
}
@@ -201,16 +202,16 @@ export const prepareState = (value: string, txState: TransactionState) => {
Object.keys(rest).forEach(field => {
const value = rest[field];
const origValue = txFields[field as keyof TxFields]
const isXrp = typeof value !== 'object' && origValue && typeof origValue === 'object' && origValue.type === 'xrp'
const isXrp = typeof value !== 'object' && origValue && typeof origValue === 'object' && origValue.$type === 'xrp'
if (isXrp) {
rest[field] = {
type: "xrp",
value: +value / 1000000, // TODO maybe use bigint?
$type: "xrp",
$value: +value / 1000000, // TODO maybe use bigint?
};
} else if (typeof value === "object") {
rest[field] = {
type: "json",
value,
$type: "json",
$value: value,
};
}
});
@@ -221,14 +222,4 @@ export const prepareState = (value: string, txState: TransactionState) => {
return tx
}
export const parseJSON = (str?: string | null): any | undefined => {
if (!str) return undefined
try {
const parsed = JSON.parse(str);
return typeof parsed === "object" ? parsed : undefined;
} catch (error) {
return undefined;
}
}
export { transactionsData }