stoplightio / json-schema-tree

Apache License 2.0
12 stars 7 forks source link

fix: nullable annotation #9

Closed mallachari closed 3 years ago

mallachari commented 3 years ago

Needed for https://github.com/stoplightio/elements/issues/1017 We still need that nullable property, thought not necessairly as a validation, annotation is good enough.

mallachari commented 3 years ago

We shouldn't need it. If there's nullable in the schema, this indicates you pass a OAS Schema Object rather than JSON Schema. You may need to run https://github.com/stoplightio/http-spec/tree/master/src/oas/transformers/schema before you provide the schema to JSV.

How would it show up in JSV SchemaNode in such case? Would it be present somewhere except fragment? We need to add null type to types array in case on nullable prop present, something like:

function getTypes(schemaNode: RegularNode): Array<SchemaNodeKind | SchemaCombinerName> {
  const types = schemaNode.types;
  if (types !== null && !types.includes(SchemaNodeKind.Null) && schemaNode.annotations.nullable) {
    types.push(SchemaNodeKind.Null);
  }
  return [types, schemaNode.combiners].reduce<Array<SchemaNodeKind | SchemaCombinerName>>((values, value) => {
    if (value === null) {
      return values;
    }

    values.push(...value);
    return values;
  }, []);
}
philsturgeon commented 3 years ago

I'm not sure if im following correctly, but for context: we don't want to show "Nullable" in the docs anymore, so we shouldn't need to worry about it?

We want to show "string or null" in the type instead of that annotation, we had an issue to remove it, but the OAS Schema > JSON Schema conversion should remove this for us. https://github.com/stoplightio/elements/issues/1017

mallachari commented 3 years ago

I'm not sure if im following correctly, but for context: we don't want to show "Nullable" in the docs anymore, so we shouldn't need to worry about it?

We want to show "string or null" in the type instead of that annotation, we had an issue to remove it, but the OAS Schema > JSON Schema conversion should remove this for us. stoplightio/elements#1017

That's exactly what I want to do. Nullable is not shown anymore in validations, but we need to add it to the types as "null" if it's present. Unless that transformer does it itself - I'll check it

P0lip commented 3 years ago

How would it show up in JSV SchemaNode in such case? Would it be present somewhere except fragment? We need to add null type to types array in case on nullable prop present, something like: Unless that transformer does it itself - I'll check it

yup, the transformer does it! The transformer turns

type: string
nullable: true

into

type:
  - string
  - null
P0lip commented 3 years ago

philsturgeon commented 3 years ago

Sweet! Job done for us. Thanks both!

mallachari commented 3 years ago

Good to know, thanks! Though currently we're passing unresolved object, along with resolver to JSV so running a transformer on such schema won't help unless we resolve it ahead in elements.