Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix checkSchema for anyOf and items array #4741

Merged
merged 1 commit into from
May 23, 2023
Merged

Fix checkSchema for anyOf and items array #4741

merged 1 commit into from
May 23, 2023

Conversation

swnf
Copy link
Contributor

@swnf swnf commented May 4, 2023

This PR contains:

  • IMPROVED TESTS
  • A BUGFIX

Describe the problem you have without this PR

The dev mode plugin throws "fieldnames do not match the regex" for fieldName:"0" if the schema contains anyOf or an items array.

My patch seems to break the (negative) test for this schema. I'm not sure how this test was supposed to work. It seems like it only worked before because checkSchema had bugs with arrays.

rxdb/test/helper/schemas.ts

Lines 625 to 650 in 397ecf2

export const wrongCompoundFormat: RxJsonSchema<CompoundIndexDocumentType> = {
title: 'compound index',
version: 0,
description: 'this schema has a compoundIndex',
keyCompression: false,
primaryKey: 'passportId',
type: 'object',
properties: {
passportId: {
type: 'string',
maxLength: 100
},
passportCountry: {
type: 'string'
},
age: {
type: 'integer'
}
},
required: [
'passportId'
],
compoundIndexes: [{
foo: 'bar'
}]
} as RxJsonSchema<CompoundIndexDocumentType>;

@pubkey
Copy link
Owner

pubkey commented May 16, 2023

This breaks the CI, please check.

@swnf
Copy link
Contributor Author

swnf commented May 16, 2023

As I wrote in my description, I don't think the tests with compoundIndexes worked before as intended. I cannot find any code in check-schema.ts that deals with the compoundIndexes key.

  1. "break when compoundIndex is specified in a separate field"

it('break when compoundIndex is specified in a separate field', () => {
assert.throws(() => checkSchema({
version: 0,
primaryKey: 'id',
type: 'object',
properties: {
id: {
type: 'string',
maxLength: 100
},
name: {
type: 'string',
index: true
} as any
},
compoundIndexes: ['id', 'name']
} as any), Error);
});

checkSchema does not throw an error because there is a compoundIndexes key. It throws an error because of the index: true. If index: true is removed, no error is thrown and the test fails.

  1. "break on wrong formatted compoundIndex"

it('break on wrong formatted compoundIndex', () => {
assert.throws(() => checkSchema(schemas.wrongCompoundFormat), Error);
});

rxdb/test/helper/schemas.ts

Lines 625 to 650 in 8261b1a

export const wrongCompoundFormat: RxJsonSchema<CompoundIndexDocumentType> = {
title: 'compound index',
version: 0,
description: 'this schema has a compoundIndex',
keyCompression: false,
primaryKey: 'passportId',
type: 'object',
properties: {
passportId: {
type: 'string',
maxLength: 100
},
passportCountry: {
type: 'string'
},
age: {
type: 'integer'
}
},
required: [
'passportId'
],
compoundIndexes: [{
foo: 'bar'
}]
} as RxJsonSchema<CompoundIndexDocumentType>;

This tests worked before because checkSchema throws the following error:

RxError (SC1): RxError (SC1):
fieldnames do not match the regex
Given parameters: {
regex:"^[a-zA-Z](?:[[a-zA-Z0-9_]*]?[a-zA-Z0-9])?$"
fieldName:"0"}

I think this error is only thrown because the fieldname check had bugs with arrays. That's why it fails in this PR because I improved the array handling.

Please tell me how the checks are supposed to handle compoundIndexes. I can also remove these tests.

pubkey added a commit that referenced this pull request May 23, 2023
@pubkey
Copy link
Owner

pubkey commented May 23, 2023

Hi @swnf
I checked and you are right. These tests are outdated. I removed them in 63f6b01

@swnf
Copy link
Contributor Author

swnf commented May 23, 2023

Thank you @pubkey. I have rebased my PR, the CI checks now pass successfully.

@pubkey pubkey merged commit 687e18c into pubkey:master May 23, 2023
pubkey added a commit that referenced this pull request May 23, 2023
@pubkey
Copy link
Owner

pubkey commented May 23, 2023

Awesome, merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants