Skip to content

Add JSON Schema validation and arithmetic operators for expressions#1000

Open
jnunemaker wants to merge 6 commits into
mainfrom
enforce-expression-types
Open

Add JSON Schema validation and arithmetic operators for expressions#1000
jnunemaker wants to merge 6 commits into
mainfrom
enforce-expression-types

Conversation

@jnunemaker

Copy link
Copy Markdown
Collaborator

Adds optional JSON Schema validation for expressions using schemas vendored from flippercloud/expressions (no npm), exposed as Expression#validate/#valid? and backed by a lazily-required, optional json_schemer dependency so core stays lightweight and unburdened on the evaluate hot path. Enforces flipper_properties value types in Gates::Expression by warning about and dropping values that aren't String/Numeric/true/false/nil, matching the schema's scalar property definition. Brings Ruby to parity with the schema by adding the Add, Subtract, Multiply, Divide, Min, Max operators and restoring Duration, which together enable relative-time windows like Now < Time(signup_at) + Duration(7, "days"). Adds spec/flipper/expression/schema_spec.rb, which runs the example JSON files vendored from the expressions repo so Ruby and JS test the same cases, plus a rake expressions:vendor task to re-copy schemas and examples.

Note: this pairs with companion changes in flippercloud/expressions (new FeatureEnabled schema, scalar property definition); it should land after that repo is published so the vendored schemas match a release.

jnunemaker and others added 2 commits June 8, 2026 10:43
Enforce expression types from a single source of truth: the JSON Schema
vendored from flippercloud/expressions.

Validation (Q2):
- Vendor schemas into lib/flipper/expression/schemas (no npm, no separate gem)
- Flipper::Expression::Schema loads them via json_schemer (2.x). The gem is an
  optional dependency, lazily required, so core stays lightweight — validation
  is a write-time tool, not needed on the evaluate hot path
- Add #validate / #valid? to Expression and Constant
- build now handles null constants, rejects multi-key hashes, and stops
  corrupting a single Hash argument via Array()

Property types (Q1):
- Gates::Expression#properties warns about and drops flipper_properties values
  that aren't String/Numeric/true/false/nil, matching the schema's scalar
  property definition, instead of mis-evaluating them

Operator parity:
- Add Add, Subtract, Multiply, Divide, Min, Max and restore Duration so Ruby
  evaluates everything the schema defines (Duration + Add enable relative-time
  windows, e.g. Now < Time(signup_at) + Duration(7, "days"))

Testing:
- spec/flipper/expression/schema_spec.rb runs the example JSON files vendored
  from the expressions repo, so Ruby and JS test the same cases
- rake expressions:vendor re-copies schemas (to lib) and examples (to spec)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The merged expressions PR dropped the `property` definition (it described
context values, not expressions). Sync the vendored copy and update the gate
comment; Q1 enforcement is unaffected since it uses ALLOWED_PROPERTY_TYPES.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@jnunemaker jnunemaker force-pushed the enforce-expression-types branch from fad402a to f366d92 Compare June 8, 2026 14:43
Reject schema-invalid expression payloads in the API before saving, and make arithmetic expression operators fail closed when operands are missing or incompatible.
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.

1 participant