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

feat(json-schema-2020-12-samples): support randomness option #9810

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

twankamp
Copy link
Contributor

@twankamp twankamp commented Apr 12, 2024

Refs #9739

Description

Add support for randomness, enabled via optionAPI and implemented in core/random.js.
Default is no randomness so everything is backward compatible.
For now only used in the minimal number of generates to enable full unit test code coverage of the changes in core/random.js. Other generators can follow in next PRs.

Open question: do we need changes to the documentation? It could make sense but I was expecting a place to already find documentation about APIs like formatAPI where we can add description for optionAPI. We can also do this later when it's actually working with all types/formats and/or exposed to users.

Motivation and Context

This is one step in #9739 to eventually support propertyNames, while also having other uses like configuration option to use randomness across the board.

How Has This Been Tested?

Unit tests have been added to cover the changes, and existing tests still pass.

Checklist

My PR contains...

  • No code changes (src/ is unmodified: changes to documentation, CI, metadata, etc.)
  • Dependency changes (any modification to dependencies in package.json)
  • Bug fixes (non-breaking change which fixes an issue)
  • Improvements (misc. changes to existing features)
  • Features (non-breaking change which adds functionality)

My changes...

  • are breaking changes to a public API (config options, System API, major UI change, etc).
  • are breaking changes to a private API (Redux, component props, utility functions, etc.).
  • are breaking changes to a developer API (npm script behavior changes, new dev system dependencies, etc).
  • are not breaking changes.

Documentation

  • My changes do not require a change to the project documentation.
  • My changes require a change to the project documentation.
  • If yes to above: I have updated the documentation accordingly.

Automated tests

  • My changes can not or do not need to be tested.
  • My changes can and should be tested by unit and/or integration tests.
  • If yes to above: I have added tests to cover my changes.
  • If yes to above: I have taken care to cover edge cases in my tests.
  • All new and existing tests passed.

@@ -4,7 +4,17 @@
import Registry from "./Registry"

class OptionRegistry extends Registry {
#defaults = {}
#defaults = {
randomEnabled: false,
Copy link
Member

Choose a reason for hiding this comment

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

These boolean properties might be confusing as they come with double boolean logic. Let's use string enum of [random, constant].

Suggested change
randomEnabled: false,
mode: 'constant',

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok makes sense. Actually nice as there could be more modes in the future.

#defaults = {
randomEnabled: false,
random: Math.random,
minInt: 0,
Copy link
Member

Choose a reason for hiding this comment

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

Let's align with json-schema-faker here:

Suggested change
minInt: 0,
minInteger: -100000000,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

randomEnabled: false,
random: Math.random,
minInt: 0,
maxInt: 2147483647,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
maxInt: 2147483647,
maxInteger: +100000000,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

random: Math.random,
minInt: 0,
maxInt: 2147483647,
minLen: 4,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
minLen: 4,
minLength: 0,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@char0n this will generate samples with empty strings by default? Is that what we want as a default behavior?

minInt: 0,
maxInt: 2147483647,
minLen: 4,
maxLen: 10,
Copy link
Member

@char0n char0n Apr 12, 2024

Choose a reason for hiding this comment

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

No upper boundary

Suggested change
maxLen: 10,
maxLength: Number.MAX_SAFE_INTEGER,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@char0n this will generate samples with extremely long strings by default? Is that what we want as a default behavior?

maxInt: 2147483647,
minLen: 4,
maxLen: 10,
minDateTime: new Date("1970-01-01T00:00:00.000Z"),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
minDateTime: new Date("1970-01-01T00:00:00.000Z"),
minDateTime: new Date("1889-12-31T00:00:00.000Z"),

maxLen: 10,
minDateTime: new Date("1970-01-01T00:00:00.000Z"),
maxDateTime: new Date("2038-01-19T00:00:00.000Z"),
maxRandExp: 100,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
maxRandExp: 100,
defaultRandExpMax: 10,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah 10 is fine which is what I initially thought of, but note that it will change the default behavior of randExp which is 100. But I think it's for the better to keep samples shorter and more readable.

@twanthony4
Copy link

@char0n let me know if this one is ok before I move to next, I think it should be good to go. Btw I changed the account where I host the fork but shouldn't affect the PR.

@twanthony4
Copy link

@char0n would you or another maintainer be able to look into merging this? I hope to implement the support for propertyNames but there are quite a few more PRs to go, so would like to get started on the next after finished this one.

Copy link
Member

@char0n char0n left a comment

Choose a reason for hiding this comment

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

Thanks! Looks good! I'll follow up with some additional changes.

@twanthony4
Copy link

@char0n @glowcloud could this one be merged? My goal as in #9739 is to add support for randomness in generators (though by default still return constants) and then use that to eventually support propertyNames which is key to full JSON Schema 2020-12 support for samples and significantly improves things like #5713. It would help if this one is merged so the next step can be built on top.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants