Skip to content

Validation vs custom properties #393

@jonthegeek

Description

@jonthegeek

My mental model is that properties are responsible for validating themselves (for the most part), but the class validator can check to make sure that the properties work with one another. However, a property with a setter breaks that model.

library(S7)
class_testing <- new_class(
  name = "testing",
  properties = list(
    first = class_character,
    second = new_property(
      setter = function(self, value) {
        # Do some things to prep value, then...
        self@second <- value
        self
      }
    )
  ),
  validator = function(self) {
    message("Validating the properties against one another")
  }
)
testing <- class_testing()
#> Validating the properties against one another
testing@second <- "this does not trigger validation"
testing@first <- "this triggers validation"
#> Validating the properties against one another

Created on 2023-12-05 with reprex v2.0.2

Is this working as intended? Is there a way to trigger overall validation after a property that has a setter is updated?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions