Skip to content

Commit

Permalink
Fix Table Schema constraints handling (#572)
Browse files Browse the repository at this point in the history
* Upgrade frictionless/typer version

* Use `ignore_constraints` for indexing

* Fixed minimum/maximum handling logic

* Don't write required false to metadata

* Fixed string pattern handling
  • Loading branch information
roll authored Sep 30, 2024
1 parent f9b2370 commit 8526492
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
50 changes: 34 additions & 16 deletions client/components/Editors/Schema/Sections/Fields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,16 @@ function Required() {
const updateField = useStore((state) => state.updateField)
const constraints = useStore(select(selectors.field, (field) => field.constraints))
const updateHelp = useStore((state) => state.updateHelp)

return (
<YesNoField
label="Required"
onFocus={() => updateHelp('schema/fields/required')}
value={constraints?.required || false}
onChange={(required) => updateField({ constraints: { ...constraints, required } })}
onChange={(value) => {
const required = value || undefined
updateField({ constraints: { ...constraints, required } })
}}
/>
)
}
Expand Down Expand Up @@ -611,12 +615,14 @@ function MinimumNumber() {
const constraints = useStore(select(selectors.field, (field) => field.constraints))
const updateHelp = useStore((state) => state.updateHelp)
const [isValid, setIsValid] = React.useState(isValidMinimumNumber())

function isValidMinimumNumber() {
if (!constraints) return true
return constraints.minimum
? validator.isNumeric(constraints.minimum.toString())
: true
}

return (
<InputField
error={!isValid}
Expand All @@ -627,9 +633,10 @@ function MinimumNumber() {
onBlur={() => {
setIsValid(isValidMinimumNumber())
}}
onChange={(value) =>
updateField({ constraints: { ...constraints, minimum: parseInt(value) } })
}
onChange={(value) => {
const minimum = value || undefined
updateField({ constraints: { ...constraints, minimum } })
}}
helperText={!isValid ? 'Minimum value is not valid.' : ''}
/>
)
Expand All @@ -640,12 +647,14 @@ function MaximumNumber() {
const constraints = useStore(select(selectors.field, (field) => field.constraints))
const updateHelp = useStore((state) => state.updateHelp)
const [isValid, setIsValid] = React.useState(isValidMaximumNumber())

function isValidMaximumNumber() {
if (!constraints) return true
return constraints.maximum
? validator.isNumeric(constraints.maximum.toString())
: true
}

return (
<InputField
error={!isValid}
Expand All @@ -656,9 +665,10 @@ function MaximumNumber() {
onBlur={() => {
setIsValid(isValidMaximumNumber())
}}
onChange={(value) =>
updateField({ constraints: { ...constraints, maximum: parseInt(value) } })
}
onChange={(value) => {
const maximum = value || undefined
updateField({ constraints: { ...constraints, maximum } })
}}
helperText={!isValid ? 'Maximum value is not valid.' : ''}
/>
)
Expand All @@ -668,15 +678,17 @@ function MinLength() {
const updateField = useStore((state) => state.updateField)
const constraints = useStore(select(selectors.field, (field) => field.constraints))
const updateHelp = useStore((state) => state.updateHelp)

return (
<InputField
type="number"
type="integer"
label="Min Length"
value={constraints?.minLength || ''}
onFocus={() => updateHelp('schema/fields/minLength')}
onChange={(value) =>
updateField({ constraints: { ...constraints, minLength: parseInt(value) } })
}
onChange={(value) => {
const minLength = value ? parseInt(value) : undefined
updateField({ constraints: { ...constraints, minLength } })
}}
/>
)
}
Expand All @@ -685,15 +697,17 @@ function MaxLength() {
const updateField = useStore((state) => state.updateField)
const constraints = useStore(select(selectors.field, (field) => field.constraints))
const updateHelp = useStore((state) => state.updateHelp)

return (
<InputField
type="number"
type="integer"
label="Max Length"
value={constraints?.maxLength || ''}
onFocus={() => updateHelp('schema/fields/maxLength')}
onChange={(value) =>
updateField({ constraints: { ...constraints, maxLength: parseInt(value) } })
}
onChange={(value) => {
const maxLength = value ? parseInt(value) : undefined
updateField({ constraints: { ...constraints, maxLength } })
}}
/>
)
}
Expand All @@ -702,13 +716,17 @@ function Pattern() {
const updateField = useStore((state) => state.updateField)
const constraints = useStore(select(selectors.field, (field) => field.constraints))
const updateHelp = useStore((state) => state.updateHelp)

return (
<InputField
type="string"
label="Pattern"
value={constraints?.pattern || ''}
onFocus={() => updateHelp('schema/fields/pattern')}
onChange={(pattern) => updateField({ constraints: { ...constraints, pattern } })}
onChange={(value) => {
const pattern = value || undefined
updateField({ constraints: { ...constraints, pattern } })
}}
/>
)
}
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ requires-python = ">=3.11"
dependencies = [
# Core
"fastapi==0.103.1",
"frictionless==5.16.0",
"frictionless==5.18.0",
"gitignore-parser==0.1.6",
"openai==0.28.0",
"pydantic==2.3.0",
"python-multipart==0.0.6",
"sqlalchemy==2.0.20",
"tinydb==4.8.0",
"typer==0.9.0",
"typer==0.12.0",
"typing_extensions==4.8.0",
"uvicorn==0.23.2",
# Gsheets
Expand Down
1 change: 1 addition & 0 deletions server/helpers/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def index_resource(project: Project, resource: Resource, table_name: str):
database=db.engine,
table_name=table_name,
with_metadata=True,
ignore_constraints=True,
)
report = indexer.index()

Expand Down

0 comments on commit 8526492

Please sign in to comment.