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

feature request: map rule #276

Closed
sbmelvin opened this issue Nov 5, 2021 · 12 comments
Closed

feature request: map rule #276

sbmelvin opened this issue Nov 5, 2021 · 12 comments

Comments

@sbmelvin
Copy link

sbmelvin commented Nov 5, 2021

Is it possible to write a schema that validates an object whose keys aren't known, but the schema of the values are?

For example:

const obj = {
  steve: {
    age: 30,
    score: 100
  },
  doug: {
    age: 40,
    score: 10
  },
  bob: {
    age: 10,
    score: 3
  }
}`

In the data I'm being provided, the keys can be any valid key name but their values are objects with a consistent schema. 
@icebob
Copy link
Owner

icebob commented Nov 6, 2021

Please explain it better.

@erfanium
Copy link
Collaborator

erfanium commented Nov 6, 2021

@icebob
he/she means something like this

schema = {
   "*": "string"
}

@icebob
Copy link
Owner

icebob commented Nov 6, 2021

Ahh, thanks. For this purpose, we use custom validators.

@erfanium
Copy link
Collaborator

erfanium commented Nov 6, 2021

@icebob I think we can introduce a new rule called Map for these kind of usages.

const schema = {
   dictionary: {
      type: "map",
      key: "string", // or number, default: string
      value: { type: "object": props: { foo: "string" } }
   }
}

@sbmelvin
Copy link
Author

sbmelvin commented Nov 6, 2021

Please explain it better.

Say I have a data source that returns the current active users of a service:

    const status = {
      timestamp: 1233113131,
      numUsers: 3,
      uptime: 201,
      users: {
        steve_carlos: {
          email: 'steve.carlos@ffffff.com',
          account_type: 'premium',
          last_login: 11244114
        },
        doug_flutey: {
          // same props as steve_carlos
        },
        mike_jones: {
          // same props
        }
      }
    }

I can't write a schema to validate this object because the keys of users change depending on which users are online. It's no problem to validate the objects within users because they have a known and unchanging schema, however validating the entire status object requires multiple schemas and multiple checks because there is currently no way to validate an object's props without explicitly listing the prop's name in the schema.

So basically I need to validate an object much in the same was as an array is validated. For example, if items is provided instead of props, iterate through the keys of the object.

@sbmelvin
Copy link
Author

sbmelvin commented Nov 6, 2021

@icebob I think we can introduce a new rule called Map for these kind of usages.

const schema = {
   dictionary: {
      type: "map",
      key: "string", // or number, default: string
      value: { type: "object": props: { foo: "string" } }
   }
}

Does type: "map" here refer to a JS Map? I ask because a key in a JS Map can be any value (Object, function, string, primitives, etc).

@erfanium
Copy link
Collaborator

erfanium commented Nov 6, 2021

@sbmelvin
You are doing it wrong. If you are the owner of the service, I recommend you just use arrays to represent online users. your response body would be like this:

    const status = {
      timestamp: 1233113131,
      numUsers: 3,
      uptime: 201,
      users: [
        {
          email: 'steve.carlos@ffffff.com',
          name: 'steve_carlos'
          account_type: 'premium',
          last_login: 11244114
        },
        {
          name: 'doug_flutey'
          // same props as steve_carlos
        },
        {
          name: 'mike_jones'
          // same props
        }
      ]
    }

That's the expected way of using JSON

@sbmelvin
Copy link
Author

sbmelvin commented Nov 6, 2021

You are doing it wrong.
That's the expected way of using JSON

That is irrelevant. This is not a style guide, it's a validator. Also, what you posted is not JSON fwiw.

If you are the owner of the service

I'm not but that is also irrelevant. I should not have to change the structure of syntactically correct javascript in order to have it validated. My data comes from drones and remote sensors that cannot be updated once deployed.

For the record, Joi and Ajv support this feature.

https://joi.dev/api/?v=17.4.2#objectpatternpattern-schema-options

https://ajv.js.org/json-schema.html#patternproperties

@sbmelvin sbmelvin closed this as completed Nov 6, 2021
@icebob icebob reopened this Nov 8, 2021
@icebob
Copy link
Owner

icebob commented Nov 8, 2021

Keep open, because the @erfanium map rule is a good idea.

@erfanium erfanium changed the title Validate object with variable keys feature request: map rule Nov 17, 2021
@askaribragimov
Copy link

+1. really strange map is not here.

@FFKL
Copy link
Contributor

FFKL commented Jun 30, 2022

@icebob As far as I know, nobody works on it. I have a similar custom rule in my project and I think I can implement it here.

@icebob
Copy link
Owner

icebob commented Jul 10, 2022

Yeah, it would be great, please open a PR with your implementation.

@FFKL FFKL mentioned this issue Jul 24, 2022
@sbmelvin sbmelvin closed this as not planned Won't fix, can't repro, duplicate, stale Oct 12, 2022
@icebob icebob reopened this Oct 12, 2022
@icebob icebob closed this as completed Oct 12, 2022
0x0a0d added a commit to 0x0a0d/fastest-validator that referenced this issue Dec 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants