Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Implement validation constraints #7

Merged
merged 1 commit into from
Feb 9, 2019
Merged

Conversation

clarencecastillo
Copy link
Member

@clarencecastillo clarencecastillo commented Feb 7, 2019

To use LOCATION_CONSTRAINTS, ORGANISATION_CONSTRAINTS, and PERSON_CONSTRAINTS, please use validate.js's validate method to validate model attributes against the defined constraints.

validate({username: "nick", password: "better"}, constraints);

For ARTWORK_CONSTRAINTS and EVENT_CONSTRAINTSuse validate.async to correctly use these constraints. These are written in an async fashion to support existential verification of referenced documents which require requests using firebase library. More info here.

validate.async(attributes, constraints).then(success, error);

@ZaynJarvis I think this will be useful in form validation when creating events or entities via ntuoss/hackoss-admin. I've defined these according to the assumptions that were presumed to be true at the time of writing but have not tested if they actually work. Need your help to check if they make sense and if they're functional.

Resolves #5. Hopefully this makes it easier to implement ntuoss/hackoss-admin#3.

Thanks!

@clarencecastillo clarencecastillo added the chore Service or data related task label Feb 7, 2019
@clarencecastillo clarencecastillo self-assigned this Feb 7, 2019
Copy link
Member

@ZaynJarvis ZaynJarvis left a comment

Choose a reason for hiding this comment

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

It's a good implementation. Thx.
Will try to work out how to implement this into admin site

@ZaynJarvis
Copy link
Member

It's a good implementation on validation.
I will look into validate.js and yup.js later for validation setting.

@ZaynJarvis
Copy link
Member

ZaynJarvis commented Feb 12, 2019

It's a good implementation.
But there are things to add in.

  1. presence doesn't work on the frontend part since all field set to default value as either '' or false. Need to set length of word to specify presence
  2. add validation on non-sense input for example name with numbers. we can use /[\w\p]+/ or more complex form for this?
  3. artwork form validation doesn't work, I think the problem relies on personExists: true
    here is the source code and errro.
Uncaught (in promise) Error: Function CollectionReference.doc() requires its first argument to be of type non-empty string, but it was: undefined
    at new FirestoreError (index.cjs.js:404)
    at validateType (index.cjs.js:730)
    at validateArgType (index.cjs.js:631)
    at CollectionReference.doc (index.cjs.js:24743)
    at PeopleRepository.<anonymous> (people.repository.js:39)
    at step (tslib.es6.js:197)
    at Object.next (tslib.es6.js:127)
    at tslib.es6.js:100
    at new Promise (<anonymous>)
    at Module.__awaiter (tslib.es6.js:77)
    at people.repository.js:32
    at new Promise (<anonymous>)
    at Function.PeopleRepository.validate_js_1.validators.personExists (people.repository.js:31)
    at Function.runValidations (validate.js:132)
    at validate (validate.js:22)
    at Object.validateArtwork [as validate] (function.js:23)
    at formik.esm.js:668
    at new Promise (<anonymous>)
    at Formik.runValidateHandler (formik.esm.js:667)
export const validateArtwork = values =>
  flattern(validate(values, ARTWORK_CONSTRAINTS));
// the framework i use doesn't allow multiple errors, so I map only the first error.
export const flattern = obj =>
  obj
    ? Object.keys(obj)
        .map(key => ({ [key]: obj[key][0] }))
        .reduce((agg, item) => ({ ...agg, ...item }))
    : {};
  1. EVENT_CONSTRAINTS has no return and no error based on the following value.
{
      venue: '',
      banner: '',
      status: 'draft',
      facebook: {
        url: '',
        status: 'draft',
        id: '',
      },
      eventbrite: {
        url: '',
        status: 'draft',
        id: '',
      },
      dependencies: [
        {
          referenceUrl: '',
          specification: '',
          label: '',
        },
      ],
      prerequisites: [
        {
          referenceUrl: '',
          proficiency: 'basic',
          label: '',
        },
      ],
      speakers: [
        {
          position: '',
          organisation: '',
          person: '',
        },
      ],
      endTime: '2019-01-01T18:30',
      startTime: '2019-01-01T18:30',
      hasDrinks: '',
      hasFood: '',
      isExternal: '',
      isPublic: '',
      remarks: '',
      promotion: '',
      description: '',
      githubUrl: '',
      tagline: '',
      title: '',
      tgif: 'd',
    }
export const validateEvent = values => {
  console.log(JSON.stringify(values, null, 2));
  const x = validate(values, EVENT_CONSTRAINTS);
  console.log(`x = ${x}`);
};

In this case console.log line hasn't been triggered.

@ZaynJarvis ZaynJarvis deleted the feature/validation branch August 1, 2020 14:55
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
chore Service or data related task
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants