Skip to content

Conditional Restrictions - Restrictions as Array, Unique as field property, No more scripts#221

Merged
joneubank merged 5 commits intofeat/209-conditional-restrictionsfrom
feat/field-restriction-array
Aug 6, 2024
Merged

Conditional Restrictions - Restrictions as Array, Unique as field property, No more scripts#221
joneubank merged 5 commits intofeat/209-conditional-restrictionsfrom
feat/field-restriction-array

Conversation

@joneubank
Copy link
Copy Markdown
Contributor

@joneubank joneubank commented Aug 1, 2024

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 unique property 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

  • All fields allow restrictions to be a single restrictions object or an array of them
  • restrictions property is still optional for all fields
  • field validation resolveRestrictions function is updated to collect restriction rules from all objects in a restriction array (if it is an array)
  • unique is now a property of a field instead of a restriction; it has been removed as a property of restriction objects
  • script has 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:

  • lectern-dictionary
    • fields are valid when restrictions is an arrays
    • fields are valid when restrictions is an object
    • fields are valid when restrictions is undefined
  • lectern-validation
    • resolveRestrictions identifies restrictions from an array
    • field validation will apply multiple restrictions of the same type

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 .

@joneubank joneubank force-pushed the feat/field-restriction-array branch from 3cf6728 to 169ea9a Compare August 1, 2024 23:02
@joneubank joneubank marked this pull request as ready for review August 1, 2024 23:02
zod.object({
valueType: zod.literal(SchemaFieldValueType.Values.string),
restrictions: StringFieldRestrictions.optional(),
restrictions: StringFieldRestrictions.or(StringFieldRestrictions.array()).optional(),
Copy link
Copy Markdown
Contributor Author

@joneubank joneubank Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single or array of StringFieldRestrictions object. This pattern is repeated for all 4 field types (integer, number, boolean).

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 {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only script fields were normalized. We have removed scripts from the Lectern Dictionary meta-schema in this PR.

return output;
}
}
return TypeUtils.asArray(field.restrictions).flatMap(extractRulesFromRestrictionObject);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convert restrictions into an array if it was a single object, simplifies processing to always be done the same way.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woohoo native flatmap

*/

import { References, replaceSchemaReferences, Schema } from '@overture-stack/lectern-dictionary';
import * as immer from 'immer';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ember anyone? ;)

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),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!undefined // true is ok?

regex: regexAlphaOnly,
},
],
} as const satisfies SchemaStringField;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@joneubank joneubank merged commit 33c8a49 into feat/209-conditional-restrictions Aug 6, 2024
@joneubank joneubank deleted the feat/field-restriction-array branch August 6, 2024 00:21
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