Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion table/table/denormalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function denormalizeTable(
const polarsSchema = getPolarsSchema(head.schema)

return table.select(
Object.values(denormalizeFields(schema, polarsSchema, options)),
...Object.values(denormalizeFields(schema, polarsSchema, options)),
)
}

Expand Down
2 changes: 1 addition & 1 deletion table/table/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function joinHeaderRows(
.withRowCount()
.withColumn(col("row_nr").add(1))
.filter(col("row_nr").add(headerOffset).isIn(headerRows))
.select(table.columns.map(name => col(name).str.concat(headerJoin)))
.select(...table.columns.map(name => col(name).str.concat(headerJoin)))
.collect()

const labels = table.columns
Expand Down
2 changes: 1 addition & 1 deletion table/table/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function normalizeTable(
const polarsSchema = getPolarsSchema(head.schema)

return table.select(
Object.values(normalizeFields(schema, polarsSchema, { noParse })),
...Object.values(normalizeFields(schema, polarsSchema, { noParse })),
)
}

Expand Down
46 changes: 44 additions & 2 deletions table/table/validate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ describe("validateTable", () => {
fieldsMatch: "equal",
fields: [
{ name: "id", type: "number" },
{ name: "name", type: "string" },
{
name: "name",
type: "string",
constraints: { required: true },
},
],
}

Expand All @@ -148,6 +152,23 @@ describe("validateTable", () => {
fieldNames: ["name"],
})
})

it("should pass when non-required fields are missing", async () => {
const table = DataFrame({
id: [1, 2],
}).lazy()

const schema: Schema = {
fieldsMatch: "equal",
fields: [
{ name: "id", type: "number" },
{ name: "name", type: "string" },
],
}

const { errors } = await validateTable(table, { schema })
expect(errors).toEqual([])
})
})

describe("fields validation with fieldsMatch='subset'", () => {
Expand Down Expand Up @@ -197,7 +218,11 @@ describe("validateTable", () => {
fieldsMatch: "subset",
fields: [
{ name: "id", type: "number" },
{ name: "name", type: "string" },
{
name: "name",
type: "string",
constraints: { required: true },
},
],
}

Expand All @@ -207,6 +232,23 @@ describe("validateTable", () => {
fieldNames: ["name"],
})
})

it("should pass when non-required fields are missing", async () => {
const table = DataFrame({
id: [1, 2],
}).lazy()

const schema: Schema = {
fieldsMatch: "subset",
fields: [
{ name: "id", type: "number" },
{ name: "name", type: "string" },
],
}

const { errors } = await validateTable(table, { schema })
expect(errors).toEqual([])
})
})

describe("fields validation with fieldsMatch='superset'", () => {
Expand Down
17 changes: 11 additions & 6 deletions table/table/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,16 @@ function validateFieldsMatch(props: {
const names = fields.map(field => field.name)
const polarsNames = polarsFields.map(field => field.name)

const requiredNames = fields
.filter(field => field.constraints?.required)
.map(field => field.name)

const extraFields = polarsFields.length - fields.length
const missingFields = fields.length - polarsFields.length

const extraNames = arrayDiff(polarsNames, names)
const missingNames = arrayDiff(names, polarsNames)
const missingRequiredNames = arrayDiff(requiredNames, polarsNames)

if (fieldsMatch === "exact") {
if (extraFields > 0) {
Expand All @@ -84,19 +89,19 @@ function validateFieldsMatch(props: {
})
}

if (missingNames.length > 0) {
if (missingRequiredNames.length > 0) {
errors.push({
type: "fields/missing",
fieldNames: missingNames,
fieldNames: missingRequiredNames,
})
}
}

if (fieldsMatch === "subset") {
if (missingNames.length > 0) {
if (missingRequiredNames.length > 0) {
errors.push({
type: "fields/missing",
fieldNames: missingNames,
fieldNames: missingRequiredNames,
})
}
}
Expand Down Expand Up @@ -147,12 +152,12 @@ async function validateFields(

let errorTable = table
.withRowCount()
.select([
.select(
col("row_nr").add(1),
lit(false).alias("error"),
...sources,
...targets,
])
)

for (const [index, field] of schema.fields.entries()) {
const polarsField = matchField(index, field, schema, polarsSchema)
Expand Down