Conditional Restrictions - Restrictions as Array, Unique as field property, No more scripts#221
Merged
joneubank merged 5 commits intofeat/209-conditional-restrictionsfrom Aug 6, 2024
Conversation
These are breaking changes to the meta schema that need to be published as a major release. A document should be added to the repo describing these changes.
3cf6728 to
169ea9a
Compare
joneubank
commented
Aug 1, 2024
| zod.object({ | ||
| valueType: zod.literal(SchemaFieldValueType.Values.string), | ||
| restrictions: StringFieldRestrictions.optional(), | ||
| restrictions: StringFieldRestrictions.or(StringFieldRestrictions.array()).optional(), |
Contributor
Author
There was a problem hiding this comment.
Single or array of StringFieldRestrictions object. This pattern is repeated for all 4 field types (integer, number, boolean).
joneubank
commented
Aug 1, 2024
Comment on lines
-40
to
-50
| function normalizeScript(input: string | string[]) { | ||
| const normalize = (script: string) => script.replace(/\r\n/g, '\n'); | ||
|
|
||
| if (typeof input === 'string') { | ||
| return normalize(input); | ||
| } else { | ||
| return input.map(normalize); | ||
| } | ||
| } | ||
|
|
||
| export function normalizeSchema(schema: Schema): Schema { |
Contributor
Author
There was a problem hiding this comment.
Only script fields were normalized. We have removed scripts from the Lectern Dictionary meta-schema in this PR.
joneubank
commented
Aug 1, 2024
| return output; | ||
| } | ||
| } | ||
| return TypeUtils.asArray(field.restrictions).flatMap(extractRulesFromRestrictionObject); |
Contributor
Author
There was a problem hiding this comment.
Convert restrictions into an array if it was a single object, simplifies processing to always be done the same way.
Contributor
There was a problem hiding this comment.
woohoo native flatmap
| */ | ||
|
|
||
| import { References, replaceSchemaReferences, Schema } from '@overture-stack/lectern-dictionary'; | ||
| import * as immer from 'immer'; |
| export const getOptionalFields = (schema: Schema): SchemaField[] => | ||
| schema.fields.filter((field) => !field.restrictions?.required); | ||
| schema.fields.filter((field) => | ||
| TypeUtils.asArray(field.restrictions).every((restrictionObject) => !restrictionObject?.required), |
Contributor
There was a problem hiding this comment.
!undefined // true is ok?| regex: regexAlphaOnly, | ||
| }, | ||
| ], | ||
| } as const satisfies SchemaStringField; |
ciaranschutte
approved these changes
Aug 5, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Objective
The primary purpose of this change is to allow fields to define restrictions as an array of restriction objects. This is a necessary step in order to allow multiple conditional restrictions.
To accomplish this, we need to separate the
uniqueproperty from the restriction array as it needs to apply to every record in a schema to make sense - ie. it can't be part of a conditional restriction, it must be found at a consistent path within each field.This also presented a good opportunity to remove script restrictions which need to be removed eventually anyways, since there is no test for scripts in the updated client, and there is no desire to rewrite the script normalization code to handle the restriction arrays.
Details
restrictionsproperty is still optional for all fieldsresolveRestrictionsfunction is updated to collect restriction rules from all objects in a restriction array (if it is an array)uniqueis now a property of a field instead of a restriction; it has been removed as a property of restriction objectsscripthas been removed as a restriction property from all field types. function normalization code that was in place to format script functions has been removed.Tests
Tests were added to check:
Tracking Meta-Schema Changes
This is the first of several breaking changes that will be made to the lectern dictionary meta-schema. All changes, breaking or additive, will be tracked in a new doc file
/docs/lectern-2.0-changes.md.