Skip to content

Commit

Permalink
fix: component attr lowerBound as empty object and type as integer
Browse files Browse the repository at this point in the history
  • Loading branch information
z0gSh1u committed Mar 27, 2024
1 parent 1bd8994 commit 4481cb3
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions packages/secretnote/src/components/component-form/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const getRenderType = (attr: Attr): string => {
case 'AT_BOOL':
return 'boolean';
case 'AT_INT':
return 'number';
return 'integer';
case 'AT_STRING':
return 'string';
case 'AT_FLOAT':
Expand All @@ -24,26 +24,33 @@ const getRenderType = (attr: Attr): string => {
};

const getValue = (attr: Attr, key: 'defaultValue' | 'lowerBound' | 'upperBound') => {
if (JSON.stringify(attr.atomic?.[key]) === '{}') {
return 0; // e.g. lowerBound: {} means lowerBound is 0
}
switch (attr.type) {
case 'AT_BOOL':
return attr.atomic?.[key]?.b;
return attr.atomic?.[key]?.b as boolean;
case 'AT_INT':
return attr.atomic?.[key]?.i64;
return attr.atomic?.[key]?.i64 as number;
case 'AT_STRING':
return attr.atomic?.[key]?.s;
return attr.atomic?.[key]?.s as string;
case 'AT_FLOAT':
return attr.atomic?.[key]?.f;
return attr.atomic?.[key]?.f as number;
}
};

const isFieldRequired = (attr: Attr) => {
return !attr.atomic.isOptional;
};

const isMinimumInclusive = (attr: Attr) => attr.atomic.lowerBoundInclusive;

const getMinimum = (attr: Attr) => {
const lowerBound = Number(getValue(attr, 'lowerBound'));
if (attr.atomic.lowerBoundEnabled) {
return Number(getValue(attr, 'lowerBound'));
return lowerBound + (isMinimumInclusive(attr) ? 0 : Number.EPSILON);
}
return undefined;
};

const getMaximum = (attr: Attr) => {
Expand Down Expand Up @@ -132,6 +139,14 @@ const transformSpecToJsonSchema: (spec: ComponentSpec) => SchemaItem = (
const { name, desc } = attr;
const type = getRenderType(attr);
const options = getOptions(attr);
const minimum = getMinimum(attr);
const $minimumMessage =
minimum === undefined
? undefined
: 'Must be greater than ' +
(isMinimumInclusive(attr)
? `or equal to ${minimum}`
: `${minimum - Number.EPSILON}`);
const attrItem: any = {
id: name,
type,
Expand All @@ -144,7 +159,8 @@ const transformSpecToJsonSchema: (spec: ComponentSpec) => SchemaItem = (
})),
$defaultValue: getValue(attr, 'defaultValue'),
$required: isFieldRequired(attr),
minimum: getMinimum(attr),
minimum,
$minimumMessage,
maximum: getMaximum(attr),
};
setByPath(json, `properties/attrs/properties/${name}`, attrItem);
Expand Down

0 comments on commit 4481cb3

Please sign in to comment.