This repository has been archived on 2024-11-11. You can view files and clone it, but cannot push or open issues or pull requests.
react-component-form/src/utils/handleCheckboxBoolean.ts

26 lines
753 B
TypeScript
Raw Normal View History

2022-08-25 23:24:40 +02:00
import type { TObject } from '@sinclair/typebox'
import type { Schema } from '../hooks/useForm'
2022-08-25 23:24:40 +02:00
export const handleCheckboxBoolean = (
object: Schema,
validateSchemaObject: TObject<Schema>
): Schema => {
2022-08-25 23:24:40 +02:00
const booleanProperties: string[] = []
for (const property in validateSchemaObject.properties) {
const rule = validateSchemaObject.properties[property]
if (rule.type === 'boolean') {
booleanProperties.push(property)
}
}
for (const booleanProperty of booleanProperties) {
if (object[booleanProperty] == null) {
object[booleanProperty] =
validateSchemaObject.properties[booleanProperty].default
} else {
object[booleanProperty] = object[booleanProperty] === 'on'
}
}
return object
}