Coder Social home page Coder Social logo

Validation about cuid HOT 12 CLOSED

paralleldrive avatar paralleldrive commented on June 20, 2024
Validation

from cuid.

Comments (12)

niftylettuce avatar niftylettuce commented on June 20, 2024 1

Doesn't isCuid method logic need to also check for 7 characters minimum? It currently does not. <https://github.com/ericelliott/cuid/blob/master/index.js#L69-L73

from cuid.

supermacro avatar supermacro commented on June 20, 2024 1

Based on this logic, the string created_at is a cuid. Perhaps there are some further rules / heuristics that could be applied in order to see if a string is a cuid?

from cuid.

MarkHerhold avatar MarkHerhold commented on June 20, 2024
  • start with c
  • cuids have 25 chars
  • cuid slugs have 7 - 10 chars

All of this is in README.md

from cuid.

ericelliott avatar ericelliott commented on June 20, 2024

For future-proofing:

  • is a string
  • starts with c
  • contains at least 7 chars

I'm open to a PR for isCuid() and isSlug() helper functions.

from cuid.

supermacro avatar supermacro commented on June 20, 2024

Screen Shot 2021-03-24 at 7 50 53 PM

from cuid.

iki avatar iki commented on June 20, 2024
// export { isCuid } from 'cuid';

// NOTE: Future cuid may change, they are only guaranteed to start with `c` and have at least 7 characters
// See https://github.com/paralleldrive/cuid/issues/88#issuecomment-339848922
// Having generation under control, we extend the check to lowercase+digits charset and 25 characters length 
const cuidRegex = /^c[a-z0-9]{24}$/;
export const isCuid = (value: string) => cuidRegex.test(value);

from cuid.

ericelliott avatar ericelliott commented on June 20, 2024

I do not recommend using a regex to validate cuids.

from cuid.

Shair17 avatar Shair17 commented on June 20, 2024

I do not recommend using a regex to validate cuids.

Why not?

from cuid.

ericelliott avatar ericelliott commented on June 20, 2024

I do not recommend using a regex to validate cuids.
Why not?

Because a small change in requirements should lead to only a small change in implementation. When your application code knows too much about what kind of ids you're using, that makes it hard to switch the id specification that you're using at a later date, if, for example you need to start using crypto wallet addresses instead of cuid-generated user ids. If your application code fails when it sees something that's not a cuid, you need to rewrite that code to make the switch.

This applies to a lot of things people try to do using classes as types - which violates the S from SOLID (the substitution principle). Proponents of class-based types will say that substitution only applies to subclasses. Those people are wrong. Liskov meant it as a principle of modularity, and she was actually pointing out a fundamental flaw in the concept of classes-as-types and subclassing things. It's a mirror of the Gang of Four's advice from "Design Patterns": "Program to an interface, not an implementation".

from cuid.

ericelliott avatar ericelliott commented on June 20, 2024

In other words, unless you're writing an implementation of CUID, you probably should not be validating the structure of a CUID in any way in your application code.

from cuid.

Hoxtygen avatar Hoxtygen commented on June 20, 2024

I do not recommend using a regex to validate cuids.
Why not?

Because a small change in requirements should lead to only a small change in implementation. When your application code knows too much about what kind of ids you're using, that makes it hard to switch the id specification that you're using at a later date, if, for example you need to start using crypto wallet addresses instead of cuid-generated user ids. If your application code fails when it sees something that's not a cuid, you need to rewrite that code to make the switch.

This applies to a lot of things people try to do using classes as types - which violates the S from SOLID (the substitution principle). Proponents of class-based types will say that substitution only applies to subclasses. Those people are wrong. Liskov meant it as a principle of modularity, and she was actually pointing out a fundamental flaw in the concept of classes-as-types and subclassing things. It's a mirror of the Gang of Four's advice from "Design Patterns": "Program to an interface, not an implementation".

I thought S is Single Responsibility Principle. Perhaps you wanted to write L which is Liskov Substitution Principle which you actually talked about.

from cuid.

ericelliott avatar ericelliott commented on June 20, 2024

👍

from cuid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.