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

Make it clear that an optional field should stay optional #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,5 @@ dist
.pnp.*

package-lock.json
.vscode
test/test-storage
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ class StructField {
if (prevField) {
if (prevField.type.fqn !== this.type.fqn) {
throw new Error(`Field was modified: ${tag}`)
} else if (prevField.required !== this.required) {
} else if (prevField.required && !this.required) {
throw new Error(`A required field must always stay required: ${tag}`)
} else if (!prevField.required && this.required) {
throw new Error(`An optional field must always stay optional: ${tag}`)
}
this.version = prevField.version
} else if (!this.struct.derived) {
Expand Down
62 changes: 62 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,68 @@ test('basic struct, one optional fields, version bump', async t => {
}
})

test('basic struct, required field to optional should fail', async t => {
const schema = await createTestSchema(t)

await schema.rebuild(schema => {
const ns = schema.namespace('test')
ns.register({
name: 'test-struct',
fields: [
{
name: 'field1',
type: 'uint',
required: true
}
]
})
})

await t.exception(schema.rebuild(schema => {
const ns = schema.namespace('test')
ns.register({
name: 'test-struct',
fields: [
{
name: 'field1',
type: 'uint'
}
]
})
}), /A required field must always stay required/)
})

test('basic struct, optional field to required should fail', async t => {
const schema = await createTestSchema(t)

await schema.rebuild(schema => {
const ns = schema.namespace('test')
ns.register({
name: 'test-struct',
fields: [
{
name: 'field1',
type: 'uint'
}
]
})
})

await t.exception(schema.rebuild(schema => {
const ns = schema.namespace('test')
ns.register({
name: 'test-struct',
fields: [
{
name: 'field1',
type: 'uint',
required: true
}
]
})
}), /An optional field must always stay optional/)
})

test('basic struct, one optional fields, type alias, version bump', async t => {
const schema = await createTestSchema(t)

Expand Down
Loading