elastic / kibana

Your window into the Elastic Stack
https://www.elastic.co/products/kibana
Other
19.64k stars 8.23k forks source link

[@kbn/config-schema] Adding transform functionality #185021

Open maryam-saeidi opened 5 months ago

maryam-saeidi commented 5 months ago

🍒 Summary

io-ts has a runtime type system for IO decoding/encoding, which allows defining an input and output type and a function to convert between these two types. This functionality enables us to not only validate an object but also convert parts of the input object to a different type.

Here is an example of Duration defined in Kibana. It receives a string like 1m and if it is valid, it returns a Duration object.

io-ts also allows us to get the whole input and output type at the top level, so type conversion can happen anywhere in the nested objects (example).

const createSLOParamsSchema = t.type({
  body: t.intersection([
    t.type({
      ...
      timeWindow: **timeWindowSchema**,
     ...
    }),
    ...
  ]),
});

const timeWindowSchema = t.union([**rollingTimeWindowSchema**, calendarAlignedTimeWindowSchema]);

const rollingTimeWindowSchema = t.type({
  duration: **durationType**, <- This is the Duration type
  type: rollingTimeWindowTypeSchema,
});

type CreateSLOInput = t.OutputOf<typeof createSLOParamsSchema.props.body>;
type CreateSLOParams = t.TypeOf<typeof createSLOParamsSchema.props.body>;

Zod seems to have a similar functionality called transform.

elasticmachine commented 5 months ago

Pinging @elastic/kibana-core (Team:Core)