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: @set expression schema validations #1273

Merged
merged 1 commit into from
Nov 1, 2023

Conversation

davenewza
Copy link
Contributor

@set attribute expression validations

The @set attribute has been almost entirely void of schema validations. In this PR we provide a bunch of support for validating these expressions.

Furthermore, improved some of the validation messaging.

Setting related data outside the scope of the input:

model User {
    fields {
        name Text
        company Company
    }
    actions {
        // This is valid
        create createUser() with (name, company.name, company.address) {
            @set(user.company.isActive = true)
        }

        // This is invalid
        create createUser() with (name, company.id) {
            @set(user.company.isActive = true)
        }

        // This is also invalid - a more complex example
        create createUser() with (
                  name, 
                  company.name, 
                  company.address, 
                  company.employees.name,
                  company.employees.country.id
                  company.employees.title)
            @set(user.company.isActive = true) // valid
            @set(user.company.employees.isActive = true) // valid
            @set(user.company.employees.country.capital = "Something") // Not valid!
        }
    }
}

Example of validation message

image

Validate against setting ctx, inputs, using multiple conditions and other nonsense:

model User {
    fields {
        name Text
    }

    actions {
        create createUser() {
            @set(ctx.identity = user.identity) // not valid!
        }

        create createUser() {
            @set(name = ctx.identity.email) // not valid!
        }

        create createUser() {
            @set("hello" = user.name) // not valid!
        }

        create createUser() {
            @set(post.published = true and post.name = "hello") // not valid!
        }

       create createUser() {
            @set(post.published) // not valid!
        }
    }
}

Examples of validation message

image

LInear tickets completed:

@davenewza davenewza requested a review from a team October 31, 2023 15:23
@davenewza davenewza changed the title fix: validation for @set expressions fix: @set expression schema validations Oct 31, 2023
chore: improved messages, cleaned up code

chore: algorithm refactor

chore: wip

chore: removed test cases
@davenewza davenewza force-pushed the fix/set-attribute-validations branch from deafe0b to a5b9189 Compare October 31, 2023 17:41
@louisaspicer
Copy link
Contributor

Basic question but, for the example:

  @set(user.company.employees.isActive = true) // valid
  @set(user.company.employees.country.capital = "Something") // Not valid!

What makes the first one valid verses the second one? is it because country is two levels deep?

@davenewza
Copy link
Contributor Author

 create createUser() with (
                  name, 
                  company.name, 
                  company.address, 
                  company.employees.name,
                  company.employees.country.id
                  company.employees.title)
            @set(user.company.isActive = true) // valid
            @set(user.company.employees.isActive = true) // valid
            @set(user.company.employees.country.capital = "Something") // Not valid!
        }

@louisaspicer if you look at the inputs of the action, you can see that we are associating country (and not creating it). So it should not be possible to set a field on an already existing record which one is referencing - I hope that makes sense

@davenewza davenewza merged commit fe5772f into main Nov 1, 2023
@davenewza davenewza deleted the fix/set-attribute-validations branch November 1, 2023 12:50
@louisaspicer
Copy link
Contributor

@louisaspicer if you look at the inputs of the action, you can see that we are associating country (and not creating it). So it should not be possible to set a field on an already existing record which one is referencing - I hope that makes sense

Ohh got it 👍 makes sense, thanks

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