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

Fields with dots in their names not receiving errors #1131

Closed
kestarumper opened this issue Jun 10, 2022 · 0 comments · Fixed by #1141
Closed

Fields with dots in their names not receiving errors #1131

kestarumper opened this issue Jun 10, 2022 · 0 comments · Fixed by #1141
Assignees
Labels
Area: Bridge Affects some of the bridge packages Bridge: JSON Schema Affects the uniforms-bridge-json-schema package Type: Bug Bug reports and their fixes
Milestone

Comments

@kestarumper
Copy link
Member

kestarumper commented Jun 10, 2022

Problem description

The issue happens with

  • "uniforms": "3.8.1",
  • "uniforms-bridge-json-schema": "3.8.1"

Individual field errors are not passed to the fields. This causes them to not display the inline error message nor highlight it (in some cases it even displays the field as "green" indicating that everything is OK). The issue happens in JSONSchemaBridge with the use of Ajv as a validator. Note: The schema validations are still performed (all invalid ones are found) and any errors are displayed by <ErrorsField />.

Technical observation

The field names (field paths, instancePath) produced by Ajv are not escaped the way uniforms do this. For example, Ajv will set the error.instancePath as "/nested.field". This causes problems in JSONSchemaBridge.getError because the escaped field name and instancePath don't match.

Reproduction

Screenshot

image

Example JSONSchema
{
  type: "object",
  properties: {
    regularRequired: {
      type: "string"
    },
    "nested.required": {
      type: "string"
    },

    regularMinLength: {
      type: "string",
      minLength: 5,
      default: "koń",
    },
    "nested.minLength": {
      type: "string",
      minLength: 5,
      default: "koń",
    }
  },
  required: ["nested.required", "regularRequired"]
}
Half-solution (semi-workaround)
import get from 'lodash/get';
import { joinName } from 'uniforms';
import JSONSchemaBridge from 'uniforms-bridge-json-schema';

function pathToName(path: string) {
  path = path.startsWith('/')
    ? path.replace(/\//g, '.').replace(/~0/g, '~').replace(/~1/g, '/')
    : path
        .replace(/\[('|")(.+?)\1\]/g, '.$2')
        .replace(/\[(.+?)\]/g, '.$1')
        .replace(/\\'/g, "'");

  return path.slice(1);
}

export class CustomJSONSchemaBridge extends JSONSchemaBridge {
  getError(name: string, error: any) {
    const details = error?.details;
    if (!Array.isArray(details)) {
      return null;
    }

    const nameParts = joinName(null, name);
    const rootName = joinName(nameParts.slice(0, -1));
    const _baseName = nameParts[nameParts.length - 1];
    const baseName = _baseName.includes('.')
      ? pathToName(_baseName)
      : _baseName;

    function findScopedError(error: any) {
      const path = pathToName(error.instancePath ?? error.dataPath);
      const _name = name.includes('.') ? pathToName(name) : name;
      const nameMatchesPath = _name === path;
      const rootMatchesPath = rootName === path;
      const baseNameMatchesMissingProperty =
        baseName === error.params.missingProperty;

      const childErrors = error.params.errors;

      const matches =
        nameMatchesPath || (rootMatchesPath && baseNameMatchesMissingProperty);

      return matches || !!childErrors?.some(findScopedError);
    }

    const scopedError = details.find(findScopedError);

    return scopedError || null;
  }
}
@kestarumper kestarumper added the Type: Bug Bug reports and their fixes label Jun 10, 2022
@kestarumper kestarumper added this to Needs triage in Open Source (migrated) via automation Jun 10, 2022
@radekmie radekmie added Area: Bridge Affects some of the bridge packages Bridge: JSON Schema Affects the uniforms-bridge-json-schema package labels Jun 10, 2022
@radekmie radekmie moved this from Needs triage to To do in Open Source (migrated) Jun 10, 2022
@radekmie radekmie added this to the v3.x milestone Jun 10, 2022
@kestarumper kestarumper self-assigned this Jun 10, 2022
@kestarumper kestarumper moved this from To do to In progress in Open Source (migrated) Jul 15, 2022
@kestarumper kestarumper moved this from In progress to Review in Open Source (migrated) Jul 15, 2022
Open Source (migrated) automation moved this from Review to Closed Jul 22, 2022
@radekmie radekmie modified the milestones: v3.x, v3.10 Oct 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Bridge Affects some of the bridge packages Bridge: JSON Schema Affects the uniforms-bridge-json-schema package Type: Bug Bug reports and their fixes
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants